home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 8 / Night Owl CD-ROM (NOPV8) (Night Owl Publisher) (1993).ISO / 017a / binutils.arj / LD.C < prev    next >
C/C++ Source or Header  |  1992-03-29  |  136KB  |  4,809 lines

  1. /* Linker `ld' for GNU
  2.    Copyright (C) 1988 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by Richard Stallman with some help from Eric Albert.
  19.    Set, indirect, and warning symbol features added by Randy Smith.  */
  20.  
  21. #ifdef GNUDOS
  22. #define TARGET -1
  23. #endif
  24.    
  25. /* Define how to initialize system-dependent header fields.  */
  26. #ifdef sun
  27. /* Use Sun's TARGET convention.  */
  28. #ifndef TARGET
  29. #define SUN2 2
  30. #define SUN3 3
  31. #define SUN4 4
  32. #if defined(sparc)
  33. #define TARGET SUN4
  34. #else
  35. #if defined(mc68020) || defined(m68020)
  36. #define TARGET SUN3
  37. #else
  38. #define TARGET SUN2
  39. #endif
  40. #endif
  41. #else
  42. #define _CROSS_TARGET_ARCH TARGET  /* locate the correct a.out.h file */
  43. #endif
  44. #endif
  45.  
  46. #include <ar.h>
  47. #include <stdio.h>
  48. #include <sys/types.h>
  49. #include <sys/stat.h>
  50. #include <sys/file.h>
  51. #include <sys/time.h>
  52. #include <sys/resource.h>
  53. #ifndef sony_news
  54. #include <fcntl.h>
  55. #endif
  56.  
  57. #ifdef COFF_ENCAPSULATE
  58. #ifdef GNUDOS
  59. #include "aoutencap.h"
  60. #else
  61. #include "a.out.encap.h"
  62. #endif
  63. #else
  64. #ifdef GNUDOS
  65. #include <aout.h>
  66. #else
  67. #include <a.out.h>
  68. #endif
  69. #endif
  70.  
  71. #ifndef N_SET_MAGIC
  72. #define N_SET_MAGIC(exec, val)  ((exec).a_magic = val)
  73. #endif
  74.  
  75. /* If compiled with GNU C, use the built-in alloca */
  76. #ifdef __GNUC__
  77. #define alloca __builtin_alloca
  78. #endif
  79.  
  80. /* Always use the GNU version of debugging symbol type codes, if possible.  */
  81.  
  82. #include "stab.h"
  83. #define CORE_ADDR unsigned long    /* For symseg.h */
  84. #include "symseg.h"
  85.  
  86. #ifdef USG
  87. #include <string.h>
  88. #else
  89. #include <strings.h>
  90. #endif
  91.  
  92. /* Determine whether we should attempt to handle (minimally)
  93.    N_BINCL and N_EINCL.  */
  94.  
  95. #if defined (__GNU_STAB__) || defined (N_BINCL)
  96. #define HAVE_SUN_STABS
  97. #endif
  98.  
  99. #define min(a,b) ((a) < (b) ? (a) : (b))
  100.  
  101. /* Macro to control the number of undefined references printed */
  102. #define MAX_UREFS_PRINTED    10
  103.  
  104. /* Size of a page; obtained from the operating system.  */
  105.  
  106. int page_size;
  107.  
  108. /* Name this program was invoked by.  */
  109.  
  110. char *progname;
  111.  
  112. /* System dependencies */
  113.  
  114. /* Define this if names etext, edata and end should not start with `_'.  */
  115. /* #define nounderscore 1 */
  116.  
  117. /* Define NON_NATIVE if using BSD or pseudo-BSD file format on a system
  118.    whose native format is different.  */
  119. /* #define NON_NATIVE */
  120.  
  121. /* Define this to specify the default executable format.  */
  122.  
  123. #ifdef hpux
  124. #define DEFAULT_MAGIC NMAGIC  /* hpux bugs screw ZMAGIC */
  125. #endif
  126.  
  127. #ifndef DEFAULT_MAGIC
  128. #define DEFAULT_MAGIC ZMAGIC
  129. #endif
  130.  
  131. /* Ordinary 4.3bsd lacks these macros in a.out.h.  */
  132.  
  133. #ifndef N_TXTADDR
  134. #if defined(vax) || defined(sony_news) || defined(hp300) || defined(pyr)
  135. #define N_TXTADDR(X) 0
  136. #endif
  137. #ifdef is68k
  138. #define N_TXTADDR(x)  (sizeof (struct exec))
  139. #endif
  140. #ifdef sequent
  141. #define    N_TXTADDR(x) (N_ADDRADJ(x))
  142. #endif
  143. #endif
  144.  
  145. #ifndef N_DATADDR
  146. #if defined(vax) || defined(sony_news) || defined(hp300) || defined(pyr)
  147. #define N_DATADDR(x) \
  148.     (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  149.     : (page_size+((N_TXTADDR(x)+(x).a_text-1) & ~(page_size-1))))
  150. #endif
  151. #ifdef is68k
  152. #define SEGMENT_SIZE 0x20000
  153. #define N_DATADDR(x) \
  154. (((x).a_magic==Omagic)? (N_TXTADDR(x)+(x).a_text) \
  155.  : (SEGMENT_SIZE + ((N_TXTADDR(x)+(x).a_text-1) & ~(SEGMENT_SIZE-1))))
  156. #endif
  157. #ifdef sequent
  158. #define N_DATADDR(x) \
  159.     (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  160.     : (page_size+(((x).a_text-1) & ~(page_size-1))))
  161. #endif
  162. #endif
  163.  
  164. #ifdef sun
  165. #if TARGET == SUN4
  166. #define INITIALIZE_HEADER \
  167. {outheader.a_machtype = M_SPARC; outheader.a_toolversion = 1;}
  168. #endif
  169. #if TARGET == SUN2
  170. #define INITIALIZE_HEADER outheader.a_machtype = M_68010
  171. #endif
  172. #ifndef INITIALIZE_HEADER
  173. #define INITIALIZE_HEADER outheader.a_machtype = M_68020
  174. #endif
  175. #define TEXT_START(x) N_PAGSIZ(x)
  176. #endif
  177. #ifdef ALTOS
  178. #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, M_68020)
  179. #endif
  180. #ifdef is68k
  181. #ifdef M_68020
  182. /* ISI rel 4.0D doesn't use it, and rel 3.05 doesn't have an
  183.    a_machtype field and so won't recognize the magic number.  To keep
  184.    binary compatibility for now, just ignore it */
  185. #define INITIALIZE_HEADER outheader.a_machtype = 0;
  186. #endif
  187. #endif
  188. #ifdef hpux
  189. #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, HP9000S200_ID)
  190. #endif
  191. #if defined(i386) && !defined(sequent)
  192. #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, M_386)
  193. #endif
  194.  
  195. #ifdef is68k
  196. /* This enables code to take care of an ugly hack in the ISI OS.
  197.    If a symbol beings with _$, then the object file is included only
  198.    if the rest of the symbol name has been referenced. */
  199. #define DOLLAR_KLUDGE
  200. #endif
  201.  
  202. /*
  203.  * Alloca include.
  204.  */
  205. #if defined(sun) && defined(sparc) && !defined(__GNUC__)
  206. #include "alloca.h"
  207. #endif
  208.  
  209. #ifndef L_SET
  210. #define L_SET 0
  211. #endif
  212.  
  213. /*
  214.  * Ok.  Following are the relocation information macros.  If your
  215.  * system should not be able to use the default set (below), you must
  216.  * define the following:
  217.  
  218.  *   relocation_info: This must be typedef'd (or #define'd) to the type
  219.  * of structure that is stored in the relocation info section of your
  220.  * a.out files.  Often this is defined in the a.out.h for your system.
  221.  *
  222.  *   RELOC_ADDRESS (rval): Offset into the current section of the
  223.  * <whatever> to be relocated.  *Must be an lvalue*.
  224.  *
  225.  *   RELOC_EXTERN_P (rval):  Is this relocation entry based on an
  226.  * external symbol (1), or was it fully resolved upon entering the
  227.  * loader (0) in which case some combination of the value in memory
  228.  * (if RELOC_MEMORY_ADD_P) and the extra (if RELOC_ADD_EXTRA) contains
  229.  * what the value of the relocation actually was.  *Must be an lvalue*.
  230.  *
  231.  *   RELOC_TYPE (rval): If this entry was fully resolved upon
  232.  * entering the loader, what type should it be relocated as?
  233.  *
  234.  *   RELOC_SYMBOL (rval): If this entry was not fully resolved upon
  235.  * entering the loader, what is the index of it's symbol in the symbol
  236.  * table?  *Must be a lvalue*.
  237.  *
  238.  *   RELOC_MEMORY_ADD_P (rval): This should return true if the final
  239.  * relocation value output here should be added to memory, or if the
  240.  * section of memory described should simply be set to the relocation
  241.  * value.
  242.  *
  243.  *   RELOC_ADD_EXTRA (rval): (Optional) This macro, if defined, gives
  244.  * an extra value to be added to the relocation value based on the
  245.  * individual relocation entry.  *Must be an lvalue if defined*.
  246.  *
  247.  *   RELOC_PCREL_P (rval): True if the relocation value described is
  248.  * pc relative.
  249.  *
  250.  *   RELOC_VALUE_RIGHTSHIFT (rval): Number of bits right to shift the
  251.  * final relocation value before putting it where it belongs.
  252.  *
  253.  *   RELOC_TARGET_SIZE (rval): log to the base 2 of the number of
  254.  * bytes of size this relocation entry describes; 1 byte == 0; 2 bytes
  255.  * == 1; 4 bytes == 2, and etc.  This is somewhat redundant (we could
  256.  * do everything in terms of the bit operators below), but having this
  257.  * macro could end up producing better code on machines without fancy
  258.  * bit twiddling.  Also, it's easier to understand/code big/little
  259.  * endian distinctions with this macro.
  260.  *
  261.  *   RELOC_TARGET_BITPOS (rval): The starting bit position within the
  262.  * object described in RELOC_TARGET_SIZE in which the relocation value
  263.  * will go.
  264.  *
  265.  *   RELOC_TARGET_BITSIZE (rval): How many bits are to be replaced
  266.  * with the bits of the relocation value.  It may be assumed by the
  267.  * code that the relocation value will fit into this many bits.  This
  268.  * may be larger than RELOC_TARGET_SIZE if such be useful.
  269.  *
  270.  *
  271.  *        Things I haven't implemented
  272.  *        ----------------------------
  273.  *
  274.  *    Values for RELOC_TARGET_SIZE other than 0, 1, or 2.
  275.  *
  276.  *    Pc relative relocation for External references.
  277.  *
  278.  *
  279.  */
  280.  
  281. /* The following #if has been modifed for cross compilation */
  282. /* It originally read:  #if defined(sun) && defined(sparc)  */
  283. /* Marc Ullman, Stanford University    Nov. 1 1989  */
  284. #if defined(sun) && (TARGET == SUN4)
  285. /* Sparc (Sun 4) macros */
  286. #undef relocation_info
  287. #define relocation_info                    reloc_info_sparc
  288. #define RELOC_ADDRESS(r)        ((r)->r_address)
  289. #define RELOC_EXTERN_P(r)               ((r)->r_extern)
  290. #define RELOC_TYPE(r)                   ((r)->r_index)
  291. #define RELOC_SYMBOL(r)                 ((r)->r_index)
  292. #define RELOC_MEMORY_SUB_P(r)        0
  293. #define RELOC_MEMORY_ADD_P(r)           0
  294. #define RELOC_ADD_EXTRA(r)              ((r)->r_addend)
  295. #define RELOC_PCREL_P(r)             \
  296.         ((r)->r_type >= RELOC_DISP8 && (r)->r_type <= RELOC_WDISP22)
  297. #define RELOC_VALUE_RIGHTSHIFT(r)       (reloc_target_rightshift[(r)->r_type])
  298. #define RELOC_TARGET_SIZE(r)            (reloc_target_size[(r)->r_type])
  299. #define RELOC_TARGET_BITPOS(r)          0
  300. #define RELOC_TARGET_BITSIZE(r)         (reloc_target_bitsize[(r)->r_type])
  301.  
  302. /* Note that these are very dependent on the order of the enums in
  303.    enum reloc_type (in a.out.h); if they change the following must be
  304.    changed */
  305. /* Also note that the last few may be incorrect; I have no information */
  306. static int reloc_target_rightshift[] = {
  307.   0, 0, 0, 0, 0, 0, 2, 2, 10, 0, 0, 0, 0, 0, 0,
  308. };
  309. static int reloc_target_size[] = {
  310.   0, 1, 2, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  311. };
  312. static int reloc_target_bitsize[] = {
  313.   8, 16, 32, 8, 16, 32, 30, 22, 22, 22, 13, 10, 32, 32, 16,
  314. };
  315.  
  316. #define    MAX_ALIGNMENT    (sizeof (double))
  317. #endif
  318. #ifdef sequent
  319. #define RELOC_ADDRESS(r)        ((r)->r_address)
  320. #define RELOC_EXTERN_P(r)        ((r)->r_extern)
  321. #define RELOC_TYPE(r)        ((r)->r_symbolnum)
  322. #define RELOC_SYMBOL(r)        ((r)->r_symbolnum)
  323. #define RELOC_MEMORY_SUB_P(r)    ((r)->r_bsr)
  324. #define RELOC_MEMORY_ADD_P(r)    1
  325. #undef RELOC_ADD_EXTRA
  326. #define RELOC_PCREL_P(r)        ((r)->r_pcrel || (r)->r_bsr)
  327. #define RELOC_VALUE_RIGHTSHIFT(r)    0
  328. #define RELOC_TARGET_SIZE(r)        ((r)->r_length)
  329. #define RELOC_TARGET_BITPOS(r)    0
  330. #define RELOC_TARGET_BITSIZE(r)    32
  331. #endif
  332.  
  333. /* Default macros */
  334. #ifndef RELOC_ADDRESS
  335. #define RELOC_ADDRESS(r)        ((r)->r_address)
  336. #define RELOC_EXTERN_P(r)        ((r)->r_extern)
  337. #define RELOC_TYPE(r)        ((r)->r_symbolnum)
  338. #define RELOC_SYMBOL(r)        ((r)->r_symbolnum)
  339. #define RELOC_MEMORY_SUB_P(r)    0
  340. #define RELOC_MEMORY_ADD_P(r)    1
  341. #undef RELOC_ADD_EXTRA
  342. #define RELOC_PCREL_P(r)        ((r)->r_pcrel)
  343. #define RELOC_VALUE_RIGHTSHIFT(r)    0
  344. #define RELOC_TARGET_SIZE(r)        ((r)->r_length)
  345. #define RELOC_TARGET_BITPOS(r)    0
  346. #define RELOC_TARGET_BITSIZE(r)    32
  347. #endif
  348.  
  349. #ifndef MAX_ALIGNMENT
  350. #define    MAX_ALIGNMENT    (sizeof (int))
  351. #endif
  352.  
  353. #ifdef nounderscore
  354. #define LPREFIX '.'
  355. #else
  356. #define LPREFIX 'L'
  357. #endif
  358.  
  359. #ifndef TEXT_START
  360. #define TEXT_START(x) N_TXTADDR(x)
  361. #endif
  362.  
  363. /* Special global symbol types understood by GNU LD.  */
  364.  
  365. /* The following type indicates the definition of a symbol as being
  366.    an indirect reference to another symbol.  The other symbol
  367.    appears as an undefined reference, immediately following this symbol.
  368.  
  369.    Indirection is asymmetrical.  The other symbol's value will be used
  370.    to satisfy requests for the indirect symbol, but not vice versa.
  371.    If the other symbol does not have a definition, libraries will
  372.    be searched to find a definition.
  373.  
  374.    So, for example, the following two lines placed in an assembler
  375.    input file would result in an object file which would direct gnu ld
  376.    to resolve all references to symbol "foo" as references to symbol
  377.    "bar".
  378.  
  379.     .stabs "_foo",11,0,0,0
  380.     .stabs "_bar",1,0,0,0
  381.  
  382.    Note that (11 == (N_INDR | N_EXT)) and (1 == (N_UNDF | N_EXT)).  */
  383.  
  384. #ifndef N_INDR
  385. #define N_INDR 0xa
  386. #endif
  387.  
  388. /* The following symbols refer to set elements.  These are expected
  389.    only in input to the loader; they should not appear in loader
  390.    output (unless relocatable output is requested).  To be recognized
  391.    by the loader, the input symbols must have their N_EXT bit set.
  392.    All the N_SET[ATDB] symbols with the same name form one set.  The
  393.    loader collects all of these elements at load time and outputs a
  394.    vector for each name.
  395.    Space (an array of 32 bit words) is allocated for the set in the
  396.    data section, and the n_value field of each set element value is
  397.    stored into one word of the array.
  398.    The first word of the array is the length of the set (number of
  399.    elements).  The last word of the vector is set to zero for possible
  400.    use by incremental loaders.  The array is ordered by the linkage
  401.    order; the first symbols which the linker encounters will be first
  402.    in the array.
  403.  
  404.    In C syntax this looks like:
  405.  
  406.     struct set_vector {
  407.       unsigned int length;
  408.       unsigned int vector[length];
  409.       unsigned int always_zero;
  410.     };
  411.  
  412.    Before being placed into the array, each element is relocated
  413.    according to its type.  This allows the loader to create an array
  414.    of pointers to objects automatically.  N_SETA type symbols will not
  415.    be relocated.
  416.  
  417.    The address of the set is made into an N_SETV symbol
  418.    whose name is the same as the name of the set.
  419.    This symbol acts like a N_DATA global symbol
  420.    in that it can satisfy undefined external references.
  421.  
  422.    For the purposes of determining whether or not to load in a library
  423.    file, set element definitions are not considered "real
  424.    definitions"; they will not cause the loading of a library
  425.    member.
  426.  
  427.    If relocatable output is requested, none of this processing is
  428.    done.  The symbols are simply relocated and passed through to the
  429.    output file.
  430.  
  431.    So, for example, the following three lines of assembler code
  432.    (whether in one file or scattered between several different ones)
  433.    will produce a three element vector (total length is five words;
  434.    see above), referenced by the symbol "_xyzzy", which will have the
  435.    addresses of the routines _init1, _init2, and _init3.
  436.  
  437.    *NOTE*: If symbolic addresses are used in the n_value field of the
  438.    defining .stabs, those symbols must be defined in the same file as
  439.    that containing the .stabs.
  440.  
  441.     .stabs "_xyzzy",23,0,0,_init1
  442.     .stabs "_xyzzy",23,0,0,_init2
  443.     .stabs "_xyzzy",23,0,0,_init3
  444.  
  445.    Note that (23 == (N_SETT | N_EXT)).  */
  446.  
  447. #ifndef N_SETA
  448. #define    N_SETA    0x14        /* Absolute set element symbol */
  449. #endif                /* This is input to LD, in a .o file.  */
  450.  
  451. #ifndef N_SETT
  452. #define    N_SETT    0x16        /* Text set element symbol */
  453. #endif                /* This is input to LD, in a .o file.  */
  454.  
  455. #ifndef N_SETD
  456. #define    N_SETD    0x18        /* Data set element symbol */
  457. #endif                /* This is input to LD, in a .o file.  */
  458.  
  459. #ifndef N_SETB
  460. #define    N_SETB    0x1A        /* Bss set element symbol */
  461. #endif                /* This is input to LD, in a .o file.  */
  462.  
  463. /* Macros dealing with the set element symbols defined in a.out.h */
  464. #define    SET_ELEMENT_P(x)    ((x)>=N_SETA&&(x)<=(N_SETB|N_EXT))
  465. #define TYPE_OF_SET_ELEMENT(x)    ((x)-N_SETA+N_ABS)
  466.  
  467. #ifndef N_SETV
  468. #define N_SETV    0x1C        /* Pointer to set vector in data area.  */
  469. #endif                /* This is output from LD.  */
  470.  
  471. /* If a this type of symbol is encountered, its name is a warning
  472.    message to print each time the symbol referenced by the next symbol
  473.    table entry is referenced.
  474.  
  475.    This feature may be used to allow backwards compatibility with
  476.    certain functions (eg. gets) but to discourage programmers from
  477.    their use.
  478.  
  479.    So if, for example, you wanted to have ld print a warning whenever
  480.    the function "gets" was used in their C program, you would add the
  481.    following to the assembler file in which gets is defined:
  482.  
  483.     .stabs "Obsolete function \"gets\" referenced",30,0,0,0
  484.     .stabs "_gets",1,0,0,0
  485.  
  486.    These .stabs do not necessarily have to be in the same file as the
  487.    gets function, they simply must exist somewhere in the compilation.  */
  488.  
  489. #ifndef N_WARNING
  490. #define N_WARNING 0x1E        /* Warning message to print if symbol
  491.                    included */
  492. #endif                /* This is input to ld */
  493.  
  494. #ifndef __GNU_STAB__
  495.  
  496. /* Line number for the data section.  This is to be used to describe
  497.    the source location of a variable declaration.  */
  498. #ifndef N_DSLINE
  499. #define N_DSLINE (N_SLINE+N_DATA-N_TEXT)
  500. #endif
  501.  
  502. /* Line number for the bss section.  This is to be used to describe
  503.    the source location of a variable declaration.  */
  504. #ifndef N_BSLINE
  505. #define N_BSLINE (N_SLINE+N_BSS-N_TEXT)
  506. #endif
  507.  
  508. #endif /* not __GNU_STAB__ */
  509.  
  510. /* Symbol table */
  511.  
  512. /* Global symbol data is recorded in these structures,
  513.    one for each global symbol.
  514.    They are found via hashing in 'symtab', which points to a vector of buckets.
  515.    Each bucket is a chain of these structures through the link field.  */
  516.  
  517. typedef
  518.   struct glosym
  519.     {
  520.       /* Pointer to next symbol in this symbol's hash bucket.  */
  521.       struct glosym *link;
  522.       /* Name of this symbol.  */
  523.       char *name;
  524.       /* Value of this symbol as a global symbol.  */
  525.       long value;
  526.       /* Chain of external 'nlist's in files for this symbol, both defs
  527.      and refs.  */
  528.       struct nlist *refs;
  529.       /* Any warning message that might be associated with this symbol
  530.          from an N_WARNING symbol encountered. */
  531.       char *warning;
  532.       /* Nonzero means definitions of this symbol as common have been seen,
  533.      and the value here is the largest size specified by any of them.  */
  534.       int max_common_size;
  535.       /* For relocatable_output, records the index of this global sym in the
  536.      symbol table to be written, with the first global sym given index 0.*/
  537.       int def_count;
  538.       /* Nonzero means a definition of this global symbol is known to exist.
  539.      Library members should not be loaded on its account.  */
  540.       char defined;
  541.       /* Nonzero means a reference to this global symbol has been seen
  542.      in a file that is surely being loaded.
  543.      A value higher than 1 is the n_type code for the symbol's
  544.      definition.  */
  545.       char referenced;
  546.       /* A count of the number of undefined references printed for a
  547.      specific symbol.  If a symbol is unresolved at the end of
  548.      digest_symbols (and the loading run is supposed to produce
  549.      relocatable output) do_file_warnings keeps track of how many
  550.      unresolved reference error messages have been printed for
  551.      each symbol here.  When the number hits MAX_UREFS_PRINTED,
  552.      messages stop. */
  553.       unsigned char undef_refs;
  554.       /* 1 means that this symbol has multiple definitions.  2 means
  555.          that it has multiple definitions, and some of them are set
  556.      elements, one of which has been printed out already.  */
  557.       unsigned char multiply_defined;
  558.       /* Nonzero means print a message at all refs or defs of this symbol */
  559.       char trace;
  560.     }
  561.   symbol;
  562.  
  563. /* Demangler for C++. */
  564. extern char *cplus_demangle ();
  565.  
  566. /* Demangler function to use. */
  567. #ifdef GNUDOS
  568. char *(*demangler)() = cplus_demangle;
  569. #else
  570. char *(*demangler)() = NULL;
  571. #endif
  572.  
  573. /* Number of buckets in symbol hash table */
  574. #define    TABSIZE    1009
  575.  
  576. /* The symbol hash table: a vector of TABSIZE pointers to struct glosym. */
  577. symbol *symtab[TABSIZE];
  578.  
  579. /* Number of symbols in symbol hash table. */
  580. int num_hash_tab_syms = 0;
  581.  
  582. /* Count the number of nlist entries that are for local symbols.
  583.    This count and the three following counts
  584.    are incremented as as symbols are entered in the symbol table.  */
  585. int local_sym_count;
  586.  
  587. /* Count number of nlist entries that are for local symbols
  588.    whose names don't start with L. */
  589. int non_L_local_sym_count;
  590.  
  591. /* Count the number of nlist entries for debugger info.  */
  592. int debugger_sym_count;
  593.  
  594. /* Count the number of global symbols referenced and not defined.  */
  595. int undefined_global_sym_count;
  596.  
  597. /* Count the number of global symbols multiply defined.  */
  598. int multiple_def_count;
  599.  
  600. /* Count the number of defined global symbols.
  601.    Each symbol is counted only once
  602.    regardless of how many different nlist entries refer to it,
  603.    since the output file will need only one nlist entry for it.
  604.    This count is computed by `digest_symbols';
  605.    it is undefined while symbols are being loaded. */
  606. int defined_global_sym_count;
  607.  
  608. /* Count the number of symbols defined through common declarations.
  609.    This count is kept in symdef_library, linear_library, and
  610.    enter_global_ref.  It is incremented when the defined flag is set
  611.    in a symbol because of a common definition, and decremented when
  612.    the symbol is defined "for real" (ie. by something besides a common
  613.    definition).  */
  614. int common_defined_global_count;
  615.  
  616. /* Count the number of set element type symbols and the number of
  617.    separate vectors which these symbols will fit into.  See the
  618.    GNU a.out.h for more info.
  619.    This count is computed by 'enter_file_symbols' */
  620. int set_symbol_count;
  621. int set_vector_count;
  622.  
  623. /* Define a linked list of strings which define symbols which should
  624.    be treated as set elements even though they aren't.  Any symbol
  625.    with a prefix matching one of these should be treated as a set
  626.    element.
  627.  
  628.    This is to make up for deficiencies in many assemblers which aren't
  629.    willing to pass any stabs through to the loader which they don't
  630.    understand.  */
  631. struct string_list_element {
  632.   char *str;
  633.   struct string_list_element *next;
  634. };
  635.  
  636. struct string_list_element *set_element_prefixes;
  637.  
  638. /* Count the number of definitions done indirectly (ie. done relative
  639.    to the value of some other symbol. */
  640. int global_indirect_count;
  641.  
  642. /* Count the number of warning symbols encountered. */
  643. int warning_count;
  644.  
  645. /* Total number of symbols to be written in the output file.
  646.    Computed by digest_symbols from the variables above.  */
  647. int nsyms;
  648.  
  649.  
  650. /* Nonzero means ptr to symbol entry for symbol to use as start addr.
  651.    -e sets this.  */
  652. symbol *entry_symbol;
  653.  
  654. symbol *edata_symbol;   /* the symbol _edata */
  655. symbol *etext_symbol;   /* the symbol _etext */
  656. symbol *end_symbol;    /* the symbol _end */
  657.  
  658. /* Each input file, and each library member ("subfile") being loaded,
  659.    has a `file_entry' structure for it.
  660.  
  661.    For files specified by command args, these are contained in the vector
  662.    which `file_table' points to.
  663.  
  664.    For library members, they are dynamically allocated,
  665.    and chained through the `chain' field.
  666.    The chain is found in the `subfiles' field of the `file_entry'.
  667.    The `file_entry' objects for the members have `superfile' fields pointing
  668.    to the one for the library.  */
  669.  
  670. struct file_entry {
  671.   /* Name of this file.  */
  672.   char *filename;
  673.   /* Name to use for the symbol giving address of text start */
  674.   /* Usually the same as filename, but for a file spec'd with -l
  675.      this is the -l switch itself rather than the filename.  */
  676.   char *local_sym_name;
  677.  
  678.   /* Describe the layout of the contents of the file */
  679.  
  680.   /* The file's a.out header.  */
  681.   struct exec header;
  682.   /* Offset in file of GDB symbol segment, or 0 if there is none.  */
  683.   int symseg_offset;
  684.  
  685.   /* Describe data from the file loaded into core */
  686.  
  687.   /* Symbol table of the file.  */
  688.   struct nlist *symbols;
  689.   /* Size in bytes of string table.  */
  690.   int string_size;
  691.   /* Pointer to the string table.
  692.      The string table is not kept in core all the time,
  693.      but when it is in core, its address is here.  */
  694.   char *strings;
  695.  
  696.   /* Next two used only if `relocatable_output' or if needed for */
  697.   /* output of undefined reference line numbers. */
  698.  
  699.   /* Text reloc info saved by `write_text' for `coptxtrel'.  */
  700.   struct relocation_info *textrel;
  701.   /* Data reloc info saved by `write_data' for `copdatrel'.  */
  702.   struct relocation_info *datarel;
  703.  
  704.   /* Relation of this file's segments to the output file */
  705.  
  706.   /* Start of this file's text seg in the output file core image.  */
  707.   int text_start_address;
  708.   /* Start of this file's data seg in the output file core image.  */
  709.   int data_start_address;
  710.   /* Start of this file's bss seg in the output file core image.  */
  711.   int bss_start_address;
  712.   /* Offset in bytes in the output file symbol table
  713.      of the first local symbol for this file.  Set by `write_file_symbols'.  */
  714.   int local_syms_offset;
  715.  
  716.   /* For library members only */
  717.  
  718.   /* For a library, points to chain of entries for the library members.  */
  719.   struct file_entry *subfiles;
  720.   /* For a library member, offset of the member within the archive.
  721.      Zero for files that are not library members.  */
  722.   int starting_offset;
  723.   /* Size of contents of this file, if library member.  */
  724.   int total_size;
  725.   /* For library member, points to the library's own entry.  */
  726.   struct file_entry *superfile;
  727.   /* For library member, points to next entry for next member.  */
  728.   struct file_entry *chain;
  729.  
  730.   /* 1 if file is a library. */
  731.   char library_flag;
  732.  
  733.   /* 1 if file's header has been read into this structure.  */
  734.   char header_read_flag;
  735.  
  736.   /* 1 means search a set of directories for this file.  */
  737.   char search_dirs_flag;
  738.  
  739.   /* 1 means this is base file of incremental load.
  740.      Do not load this file's text or data.
  741.      Also default text_start to after this file's bss. */
  742.   char just_syms_flag;
  743. };
  744.  
  745. /* Vector of entries for input files specified by arguments.
  746.    These are all the input files except for members of specified libraries.  */
  747. struct file_entry *file_table;
  748.  
  749. /* Length of that vector.  */
  750. int number_of_files;
  751.  
  752. /* When loading the text and data, we can avoid doing a close
  753.    and another open between members of the same library.
  754.  
  755.    These two variables remember the file that is currently open.
  756.    Both are zero if no file is open.
  757.  
  758.    See `each_file' and `file_close'.  */
  759.  
  760. struct file_entry *input_file;
  761. int input_desc;
  762.  
  763. /* The name of the file to write; "a.out" by default.  */
  764.  
  765. char *output_filename;
  766.  
  767. /* Descriptor for writing that file with `mywrite'.  */
  768.  
  769. int outdesc;
  770.  
  771. /* Header for that file (filled in by `write_header').  */
  772.  
  773. struct exec outheader;
  774.  
  775. #ifdef COFF_ENCAPSULATE
  776. struct coffheader coffheader;
  777. int need_coff_header;
  778. #endif
  779.  
  780. /* The following are computed by `digest_symbols'.  */
  781.  
  782. int text_size;        /* total size of text of all input files.  */
  783. int data_size;        /* total size of data of all input files.  */
  784. int bss_size;        /* total size of bss of all input files.  */
  785. int text_reloc_size;    /* total size of text relocation of all input files.  */
  786. int data_reloc_size;    /* total size of data relocation of all input */
  787.             /* files.  */
  788.  
  789. /* Specifications of start and length of the area reserved at the end
  790.    of the text segment for the set vectors.  Computed in 'digest_symbols' */
  791. int set_sect_start;
  792. int set_sect_size;
  793.  
  794. /* Pointer for in core storage for the above vectors, before they are
  795.    written. */
  796. unsigned long *set_vectors;
  797.  
  798. /* Amount of cleared space to leave between the text and data segments.  */
  799.  
  800. int text_pad;
  801.  
  802. /* Amount of bss segment to include as part of the data segment.  */
  803.  
  804. int data_pad;
  805.  
  806. /* Format of __.SYMDEF:
  807.    First, a longword containing the size of the 'symdef' data that follows.
  808.    Second, zero or more 'symdef' structures.
  809.    Third, a longword containing the length of symbol name strings.
  810.    Fourth, zero or more symbol name strings (each followed by a null).  */
  811.  
  812. struct symdef {
  813.   int symbol_name_string_index;
  814.   int library_member_offset;
  815. };
  816.  
  817. /* Record most of the command options.  */
  818.  
  819. /* Address we assume the text section will be loaded at.
  820.    We relocate symbols and text and data for this, but we do not
  821.    write any padding in the output file for it.  */
  822. int text_start;
  823.  
  824. /* Offset of default entry-pc within the text section.  */
  825. int entry_offset;
  826.  
  827. /* Address we decide the data section will be loaded at.  */
  828. int data_start;
  829.  
  830. /* `text-start' address is normally this much plus a page boundary.
  831.    This is not a user option; it is fixed for each system.  */
  832. int text_start_alignment;
  833.  
  834. /* Nonzero if -T was specified in the command line.
  835.    This prevents text_start from being set later to default values.  */
  836. int T_flag_specified;
  837.  
  838. /* Nonzero if -Tdata was specified in the command line.
  839.    This prevents data_start from being set later to default values.  */
  840. int Tdata_flag_specified;
  841.  
  842. /* Size to pad data section up to.
  843.    We simply increase the size of the data section, padding with zeros,
  844.    and reduce the size of the bss section to match.  */
  845. int specified_data_size;
  846.  
  847. /* Magic number to use for the output file, set by switch.  */
  848. int magic;
  849.  
  850. /* Nonzero means print names of input files as processed.  */
  851. int trace_files;
  852.  
  853. /* Which symbols should be stripped (omitted from the output):
  854.    none, all, or debugger symbols.  */
  855. enum { STRIP_NONE, STRIP_ALL, STRIP_DEBUGGER } strip_symbols;
  856.  
  857. /* Which local symbols should be omitted:
  858.    none, all, or those starting with L.
  859.    This is irrelevant if STRIP_NONE.  */
  860. enum { DISCARD_NONE, DISCARD_ALL, DISCARD_L } discard_locals;
  861.  
  862. /* 1 => write load map.  */
  863. int write_map;
  864.  
  865. /* 1 => write relocation into output file so can re-input it later.  */
  866. int relocatable_output;
  867.  
  868. /* 1 => assign space to common symbols even if `relocatable_output'.  */
  869. int force_common_definition;
  870.  
  871. /* Standard directories to search for files specified by -l.  */
  872. char *standard_search_dirs[] =
  873. #ifdef STANDARD_SEARCH_DIRS
  874.   {STANDARD_SEARCH_DIRS};
  875. #else
  876. #ifdef NON_NATIVE
  877.   {"/usr/local/lib/gnu"};
  878. #else
  879.   {"/lib", "/usr/lib", "/usr/local/lib"};
  880. #endif
  881. #endif
  882.  
  883. /* Actual vector of directories to search;
  884.    this contains those specified with -L plus the standard ones.  */
  885. char **search_dirs;
  886.  
  887. /* Length of the vector `search_dirs'.  */
  888. int n_search_dirs;
  889.  
  890. /* Non zero means to create the output executable. */
  891. /* Cleared by nonfatal errors.  */
  892. int make_executable;
  893.  
  894. /* Force the executable to be output, even if there are non-fatal
  895.    errors */
  896. int force_executable;
  897.  
  898. /* Keep a list of any symbols referenced from the command line (so
  899.    that error messages for these guys can be generated). This list is
  900.    zero terminated. */
  901. struct glosym **cmdline_references;
  902. int cl_refs_allocated;
  903.  
  904. #ifndef GNUDOS
  905. void bcopy (), bzero ();
  906. int malloc (), realloc ();
  907. #ifndef alloca
  908. int alloca ();
  909. #endif
  910. int free ();
  911. #endif
  912.  
  913. int xmalloc ();
  914. int xrealloc ();
  915. void fatal ();
  916. void fatal_with_file ();
  917. void perror_name ();
  918. void perror_file ();
  919. void error ();
  920.  
  921. void digest_symbols ();
  922. void print_symbols ();
  923. void load_symbols ();
  924. void decode_command ();
  925. void list_undefined_symbols ();
  926. void list_unresolved_references ();
  927. void write_output ();
  928. void write_header ();
  929. void write_text ();
  930. void read_file_relocation ();
  931. void write_data ();
  932. void write_rel ();
  933. void write_syms ();
  934. void write_symsegs ();
  935. void mywrite ();
  936. void symtab_init ();
  937. void padfile ();
  938. char *concat ();
  939. char *get_file_name ();
  940. symbol *getsym (), *getsym_soft ();
  941.  
  942. int
  943. main (argc, argv)
  944.      char **argv;
  945.      int argc;
  946. {
  947. /*   Added this to stop ld core-dumping on very large .o files.    */
  948. #ifdef RLIMIT_STACK
  949.   /* Get rid of any avoidable limit on stack size.  */
  950.   {
  951.     struct rlimit rlim;
  952.  
  953.     /* Set the stack limit huge so that alloca does not fail. */
  954.     getrlimit (RLIMIT_STACK, &rlim);
  955.     rlim.rlim_cur = rlim.rlim_max;
  956.     setrlimit (RLIMIT_STACK, &rlim);
  957.   }
  958. #endif /* RLIMIT_STACK */
  959.  
  960. #ifdef GNUDOS
  961.   _fmode = O_BINARY; /* set default file type */
  962. #endif
  963.  
  964.   page_size = getpagesize ();
  965.   progname = argv[0];
  966.  
  967.   /* Clear the cumulative info on the output file.  */
  968.  
  969.   text_size = 0;
  970.   data_size = 0;
  971.   bss_size = 0;
  972.   text_reloc_size = 0;
  973.   data_reloc_size = 0;
  974.  
  975.   data_pad = 0;
  976.   text_pad = 0;
  977.  
  978.   /* Initialize the data about options.  */
  979.  
  980.   specified_data_size = 0;
  981.   strip_symbols = STRIP_NONE;
  982.   trace_files = 0;
  983.   discard_locals = DISCARD_NONE;
  984.   entry_symbol = 0;
  985.   write_map = 0;
  986.   relocatable_output = 0;
  987.   force_common_definition = 0;
  988.   T_flag_specified = 0;
  989.   Tdata_flag_specified = 0;
  990.   magic = DEFAULT_MAGIC;
  991.   make_executable = 1;
  992.   force_executable = 0;
  993.   set_element_prefixes = 0;
  994.  
  995.   /* Initialize the cumulative counts of symbols.  */
  996.  
  997.   local_sym_count = 0;
  998.   non_L_local_sym_count = 0;
  999.   debugger_sym_count = 0;
  1000.   undefined_global_sym_count = 0;
  1001.   set_symbol_count = 0;
  1002.   set_vector_count = 0;
  1003.   global_indirect_count = 0;
  1004.   warning_count = 0;
  1005.   multiple_def_count = 0;
  1006.   common_defined_global_count = 0;
  1007.  
  1008.   /* Keep a list of symbols referenced from the command line */
  1009.   cl_refs_allocated = 10;
  1010.   cmdline_references
  1011.     = (struct glosym **) xmalloc (cl_refs_allocated
  1012.                   * sizeof(struct glosym *));
  1013.   *cmdline_references = 0;
  1014.  
  1015.   /* Completely decode ARGV.  */
  1016.  
  1017.   decode_command (argc, argv);
  1018.  
  1019.   /* Create the symbols `etext', `edata' and `end'.  */
  1020.  
  1021.   if (!relocatable_output)
  1022.     symtab_init ();
  1023.  
  1024.   /* Determine whether to count the header as part of
  1025.      the text size, and initialize the text size accordingly.
  1026.      This depends on the kind of system and on the output format selected.  */
  1027.  
  1028.   N_SET_MAGIC (outheader, magic);
  1029. #ifdef INITIALIZE_HEADER
  1030.   INITIALIZE_HEADER;
  1031. #endif
  1032.  
  1033.   text_size = sizeof (struct exec);
  1034. #ifdef COFF_ENCAPSULATE
  1035.   if (relocatable_output == 0 && file_table[0].just_syms_flag == 0)
  1036.     {
  1037.       need_coff_header = 1;
  1038.       /* set this flag now, since it will change the values of N_TXTOFF, etc */
  1039.       N_SET_FLAGS (outheader, N_FLAGS_COFF_ENCAPSULATE);
  1040.       text_size += sizeof (struct coffheader);
  1041.     }
  1042. #endif
  1043.  
  1044.   text_size -= N_TXTOFF (outheader);
  1045.  
  1046.   if (text_size < 0)
  1047.     text_size = 0;
  1048.   entry_offset = text_size;
  1049.  
  1050.   if (!T_flag_specified && !relocatable_output)
  1051.     text_start = TEXT_START (outheader);
  1052.  
  1053.   /* The text-start address is normally this far past a page boundary.  */
  1054.   text_start_alignment = text_start % page_size;
  1055.  
  1056.   /* Load symbols of all input files.
  1057.      Also search all libraries and decide which library members to load.  */
  1058.  
  1059.   load_symbols ();
  1060.  
  1061.   /* Compute where each file's sections go, and relocate symbols.  */
  1062.  
  1063.   digest_symbols ();
  1064.  
  1065.   /* Print error messages for any missing symbols, for any warning
  1066.      symbols, and possibly multiple definitions */
  1067.  
  1068.   do_warnings (stderr);
  1069.  
  1070.   /* Print a map, if requested.  */
  1071.  
  1072.   if (write_map) print_symbols (stdout);
  1073.  
  1074.   /* Write the output file.  */
  1075.  
  1076.   if (make_executable || force_executable)
  1077.     write_output ();
  1078.  
  1079.   exit (!make_executable);
  1080. }
  1081.  
  1082. void decode_option ();
  1083.  
  1084. /* Analyze a command line argument.
  1085.    Return 0 if the argument is a filename.
  1086.    Return 1 if the argument is a option complete in itself.
  1087.    Return 2 if the argument is a option which uses an argument.
  1088.  
  1089.    Thus, the value is the number of consecutive arguments
  1090.    that are part of options.  */
  1091.  
  1092. int
  1093. classify_arg (arg)
  1094.      register char *arg;
  1095. {
  1096.   if (*arg != '-') return 0;
  1097.   switch (arg[1])
  1098.     {
  1099.     case 'A':
  1100.     case 'D':
  1101.     case 'e':
  1102.     case 'L':
  1103.     case 'l':
  1104.     case 'o':
  1105.     case 'u':
  1106.     case 'V':
  1107.     case 'y':
  1108.       if (arg[2])
  1109.     return 1;
  1110.       return 2;
  1111.  
  1112.     case 'B':
  1113.       if (! strcmp (&arg[2], "static"))
  1114.     return 1;
  1115.  
  1116.     case 'T':
  1117.       if (arg[2] == 0)
  1118.     return 2;
  1119.       if (! strcmp (&arg[2], "text"))
  1120.     return 2;
  1121.       if (! strcmp (&arg[2], "data"))
  1122.     return 2;
  1123.       return 1;
  1124.     }
  1125.  
  1126.   return 1;
  1127. }
  1128.  
  1129. /* Process the command arguments,
  1130.    setting up file_table with an entry for each input file,
  1131.    and setting variables according to the options.  */
  1132.  
  1133. void
  1134. decode_command (argc, argv)
  1135.      char **argv;
  1136.      int argc;
  1137. {
  1138.   register int i;
  1139.   register struct file_entry *p;
  1140.  
  1141.   number_of_files = 0;
  1142.   output_filename = "a.out";
  1143.  
  1144.   n_search_dirs = 0;
  1145.   search_dirs = (char **) xmalloc (sizeof (char *));
  1146.  
  1147.   /* First compute number_of_files so we know how long to make file_table.  */
  1148.   /* Also process most options completely.  */
  1149.  
  1150.   for (i = 1; i < argc; i++)
  1151.     {
  1152.       register int code = classify_arg (argv[i]);
  1153.       if (code)
  1154.     {
  1155.       if (i + code > argc)
  1156.         fatal ("no argument following %s\n", argv[i]);
  1157.  
  1158.       decode_option (argv[i], argv[i+1]);
  1159.  
  1160.       if (argv[i][1] == 'l' || argv[i][1] == 'A')
  1161.         number_of_files++;
  1162.  
  1163.       i += code - 1;
  1164.     }
  1165.       else
  1166.     number_of_files++;
  1167.     }
  1168.  
  1169.   if (!number_of_files)
  1170.     fatal ("no input files", 0);
  1171.  
  1172.   p = file_table
  1173.     = (struct file_entry *) xmalloc (number_of_files * sizeof (struct file_entry));
  1174.   bzero (p, number_of_files * sizeof (struct file_entry));
  1175.  
  1176.   /* Now scan again and fill in file_table.  */
  1177.   /* All options except -A and -l are ignored here.  */
  1178.  
  1179.   for (i = 1; i < argc; i++)
  1180.     {
  1181.       register int code = classify_arg (argv[i]);
  1182.  
  1183.       if (code)
  1184.     {
  1185.       char *string;
  1186.       if (code == 2)
  1187.         string = argv[i+1];
  1188.       else
  1189.         string = &argv[i][2];
  1190.  
  1191.       if (argv[i][1] == 'A')
  1192.         {
  1193.           if (p != file_table)
  1194.         fatal ("-A specified before an input file other than the first");
  1195.  
  1196.           p->filename = string;
  1197.           p->local_sym_name = string;
  1198.           p->just_syms_flag = 1;
  1199.           p++;
  1200.         }
  1201.       if (argv[i][1] == 'l')
  1202.         {
  1203.           p->filename = concat ("lib", string, ".a");
  1204.           p->local_sym_name = concat ("-l", string, "");
  1205.           p->search_dirs_flag = 1;
  1206.           p++;
  1207.         }
  1208.       i += code - 1;
  1209.     }
  1210.       else
  1211.     {
  1212.       p->filename = argv[i];
  1213.       p->local_sym_name = argv[i];
  1214.       p++;
  1215.     }
  1216.     }
  1217.  
  1218.   /* Now check some option settings for consistency.  */
  1219.  
  1220. #ifdef NMAGIC
  1221.   if ((magic == ZMAGIC || magic == NMAGIC)
  1222. #else
  1223.   if ((magic == ZMAGIC)
  1224. #endif
  1225.       && (text_start - text_start_alignment) & (page_size - 1))
  1226.     fatal ("-T argument not multiple of page size, with sharable output", 0);
  1227.  
  1228.   /* Append the standard search directories to the user-specified ones.  */
  1229.   {
  1230.     int n = sizeof standard_search_dirs / sizeof standard_search_dirs[0];
  1231. #ifdef GNUDOS
  1232.     extern char *getenv();
  1233.     int libcnt;
  1234.     char *lib_env = getenv("GCCLIB");
  1235.     if (lib_env)
  1236.       for (libcnt = 0; libcnt < n; libcnt++)
  1237.         standard_search_dirs[libcnt] = lib_env;
  1238. #endif
  1239.     n_search_dirs += n;
  1240.     search_dirs
  1241.       = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  1242.     bcopy (standard_search_dirs, &search_dirs[n_search_dirs - n],
  1243.        n * sizeof (char *));
  1244.   }
  1245. }
  1246.  
  1247.  
  1248. void
  1249. add_cmdline_ref (sp)
  1250.      struct glosym *sp;
  1251. {
  1252.   struct glosym **ptr;
  1253.  
  1254.   for (ptr = cmdline_references;
  1255.        ptr < cmdline_references + cl_refs_allocated && *ptr;
  1256.        ptr++)
  1257.     ;
  1258.  
  1259.   if (ptr >= cmdline_references + cl_refs_allocated - 1)
  1260.     {
  1261.       int diff = ptr - cmdline_references;
  1262.  
  1263.       cl_refs_allocated *= 2;
  1264.       cmdline_references = (struct glosym **)
  1265.     xrealloc (cmdline_references,
  1266.          cl_refs_allocated * sizeof (struct glosym *));
  1267.       ptr = cmdline_references + diff;
  1268.     }
  1269.  
  1270.   *ptr++ = sp;
  1271.   *ptr = (struct glosym *) 0;
  1272. }
  1273.  
  1274. int
  1275. set_element_prefixed_p (name)
  1276.      char *name;
  1277. {
  1278.   struct string_list_element *p;
  1279.   int i;
  1280.  
  1281.   for (p = set_element_prefixes; p; p = p->next)
  1282.     {
  1283.       for (i = 0; p->str[i] != '\0' && (p->str[i] == name[i]); i++)
  1284.     ;
  1285.  
  1286.       if (p->str[i] == '\0')
  1287.     return 1;
  1288.     }
  1289.   return 0;
  1290. }
  1291.  
  1292. int parse ();
  1293.  
  1294. /* Record an option and arrange to act on it later.
  1295.    ARG should be the following command argument,
  1296.    which may or may not be used by this option.
  1297.  
  1298.    The `l' and `A' options are ignored here since they actually
  1299.    specify input files.  */
  1300.  
  1301. void
  1302. decode_option (swt, arg)
  1303.      register char *swt, *arg;
  1304. {
  1305.   /* We get Bstatic from gcc on suns.  */
  1306.   if (! strcmp (swt + 1, "Bstatic"))
  1307.     return;
  1308.   if (! strcmp (swt + 1, "Ttext"))
  1309.     {
  1310.       text_start = parse (arg, "%x", "invalid argument to -Ttext");
  1311.       T_flag_specified = 1;
  1312.       return;
  1313.     }
  1314.   if (! strcmp (swt + 1, "Tdata"))
  1315.     {
  1316.       data_start = parse (arg, "%x", "invalid argument to -Tdata");
  1317.       Tdata_flag_specified = 1;
  1318.       return;
  1319.     }
  1320.   if (! strcmp (swt + 1, "noinhibit-exec"))
  1321.     {
  1322.       force_executable = 1;
  1323.       return;
  1324.     }
  1325.  
  1326.   if (swt[2] != 0)
  1327.     arg = &swt[2];
  1328.  
  1329.   switch (swt[1])
  1330.     {
  1331.     case 'A':
  1332.       return;
  1333.  
  1334.     case 'D':
  1335.       specified_data_size = parse (arg, "%x", "invalid argument to -D");
  1336.       return;
  1337.  
  1338.     case 'd':
  1339.       force_common_definition = 1;
  1340.       return;
  1341.  
  1342.     case 'e':
  1343.       entry_symbol = getsym (arg);
  1344.       if (!entry_symbol->defined && !entry_symbol->referenced)
  1345.     undefined_global_sym_count++;
  1346.       entry_symbol->referenced = 1;
  1347.       add_cmdline_ref (entry_symbol);
  1348.       return;
  1349.  
  1350.     case 'l':
  1351.       /* If linking with libg++, use the C++ demangler. */
  1352.       if (arg != NULL && strcmp (arg, "g++") == 0)
  1353.     demangler = cplus_demangle;
  1354.       return;
  1355.  
  1356.     case 'L':
  1357.       n_search_dirs++;
  1358.       search_dirs
  1359.     = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  1360.       search_dirs[n_search_dirs - 1] = arg;
  1361.       return;
  1362.  
  1363.     case 'M':
  1364.       write_map = 1;
  1365.       return;
  1366.  
  1367.     case 'N':
  1368.       magic = OMAGIC;
  1369.       return;
  1370.  
  1371. #ifdef NMAGIC
  1372.     case 'n':
  1373.       magic = NMAGIC;
  1374.       return;
  1375. #endif
  1376.  
  1377.     case 'o':
  1378.       output_filename = arg;
  1379.       return;
  1380.  
  1381.     case 'r':
  1382.       relocatable_output = 1;
  1383.       magic = OMAGIC;
  1384.       text_start = 0;
  1385.       return;
  1386.  
  1387.     case 'S':
  1388.       strip_symbols = STRIP_DEBUGGER;
  1389.       return;
  1390.  
  1391.     case 's':
  1392.       strip_symbols = STRIP_ALL;
  1393.       return;
  1394.  
  1395.     case 'T':
  1396.       text_start = parse (arg, "%x", "invalid argument to -T");
  1397.       T_flag_specified = 1;
  1398.       return;
  1399.  
  1400.     case 't':
  1401.       trace_files = 1;
  1402.       return;
  1403.  
  1404.     case 'u':
  1405.       {
  1406.     register symbol *sp = getsym (arg);
  1407.     if (!sp->defined && !sp->referenced)
  1408.       undefined_global_sym_count++;
  1409.     sp->referenced = 1;
  1410.     add_cmdline_ref (sp);
  1411.       }
  1412.       return;
  1413.  
  1414.     case 'V':
  1415.       {
  1416.     struct string_list_element *new
  1417.       = (struct string_list_element *)
  1418.         xmalloc (sizeof (struct string_list_element));
  1419.  
  1420.     new->str = arg;
  1421.     new->next = set_element_prefixes;
  1422.     set_element_prefixes = new;
  1423.     return;
  1424.       }
  1425.  
  1426.     case 'X':
  1427.       discard_locals = DISCARD_L;
  1428.       return;
  1429.  
  1430.     case 'x':
  1431.       discard_locals = DISCARD_ALL;
  1432.       return;
  1433.  
  1434.     case 'y':
  1435.       {
  1436.     register symbol *sp = getsym (&swt[2]);
  1437.     sp->trace = 1;
  1438.       }
  1439.       return;
  1440.  
  1441.     case 'z':
  1442.       magic = ZMAGIC;
  1443.       return;
  1444.  
  1445.     default:
  1446.       fatal ("invalid command option `%s'", swt);
  1447.     }
  1448. }
  1449.  
  1450. /** Convenient functions for operating on one or all files being */
  1451.  /** loaded.  */
  1452. void print_file_name ();
  1453.  
  1454. /* Call FUNCTION on each input file entry.
  1455.    Do not call for entries for libraries;
  1456.    instead, call once for each library member that is being loaded.
  1457.  
  1458.    FUNCTION receives two arguments: the entry, and ARG.  */
  1459.  
  1460. void
  1461. each_file (function, arg)
  1462.      register void (*function)();
  1463.      register int arg;
  1464. {
  1465.   register int i;
  1466.  
  1467.   for (i = 0; i < number_of_files; i++)
  1468.     {
  1469.       register struct file_entry *entry = &file_table[i];
  1470.       if (entry->library_flag)
  1471.         {
  1472.       register struct file_entry *subentry = entry->subfiles;
  1473.       for (; subentry; subentry = subentry->chain)
  1474.         (*function) (subentry, arg);
  1475.     }
  1476.       else
  1477.     (*function) (entry, arg);
  1478.     }
  1479. }
  1480.  
  1481. /* Call FUNCTION on each input file entry until it returns a non-zero
  1482.    value.  Return this value.
  1483.    Do not call for entries for libraries;
  1484.    instead, call once for each library member that is being loaded.
  1485.  
  1486.    FUNCTION receives two arguments: the entry, and ARG.  It must be a
  1487.    function returning unsigned long (though this can probably be fudged). */
  1488.  
  1489. unsigned long
  1490. check_each_file (function, arg)
  1491.      register unsigned long (*function)();
  1492.      register int arg;
  1493. {
  1494.   register int i;
  1495.   register unsigned long return_val;
  1496.  
  1497.   for (i = 0; i < number_of_files; i++)
  1498.     {
  1499.       register struct file_entry *entry = &file_table[i];
  1500.       if (entry->library_flag)
  1501.         {
  1502.       register struct file_entry *subentry = entry->subfiles;
  1503.       for (; subentry; subentry = subentry->chain)
  1504.         if (return_val = (*function) (subentry, arg))
  1505.           return return_val;
  1506.     }
  1507.       else
  1508.     if (return_val = (*function) (entry, arg))
  1509.       return return_val;
  1510.     }
  1511.   return 0;
  1512. }
  1513.  
  1514. /* Like `each_file' but ignore files that were just for symbol definitions.  */
  1515.  
  1516. void
  1517. each_full_file (function, arg)
  1518.      register void (*function)();
  1519.      register int arg;
  1520. {
  1521.   register int i;
  1522.  
  1523.   for (i = 0; i < number_of_files; i++)
  1524.     {
  1525.       register struct file_entry *entry = &file_table[i];
  1526.       if (entry->just_syms_flag)
  1527.     continue;
  1528.       if (entry->library_flag)
  1529.         {
  1530.       register struct file_entry *subentry = entry->subfiles;
  1531.       for (; subentry; subentry = subentry->chain)
  1532.         (*function) (subentry, arg);
  1533.     }
  1534.       else
  1535.     (*function) (entry, arg);
  1536.     }
  1537. }
  1538.  
  1539. /* Close the input file that is now open.  */
  1540.  
  1541. void
  1542. file_close ()
  1543. {
  1544.   close (input_desc);
  1545.   input_desc = 0;
  1546.   input_file = 0;
  1547. }
  1548.  
  1549. /* Open the input file specified by 'entry', and return a descriptor.
  1550.    The open file is remembered; if the same file is opened twice in a row,
  1551.    a new open is not actually done.  */
  1552.  
  1553. int
  1554. file_open (entry)
  1555.      register struct file_entry *entry;
  1556. {
  1557.   register int desc;
  1558.  
  1559.   if (entry->superfile)
  1560.     return file_open (entry->superfile);
  1561.  
  1562.   if (entry == input_file)
  1563.     return input_desc;
  1564.  
  1565.   if (input_file) file_close ();
  1566.  
  1567.   if (entry->search_dirs_flag)
  1568.     {
  1569.       int i;
  1570.  
  1571.       for (i = 0; i < n_search_dirs; i++)
  1572.     {
  1573.       register char *string
  1574.         = concat (search_dirs[i], "/", entry->filename);
  1575.       desc = open (string, O_RDONLY, 0);
  1576.       if (desc > 0)
  1577.         {
  1578.           entry->filename = string;
  1579.           entry->search_dirs_flag = 0;
  1580.           break;
  1581.         }
  1582.       free (string);
  1583.     }
  1584.     }
  1585.   else
  1586.     desc = open (entry->filename, O_RDONLY, 0);
  1587.  
  1588.   if (desc > 0)
  1589.     {
  1590.       input_file = entry;
  1591.       input_desc = desc;
  1592.       return desc;
  1593.     }
  1594.  
  1595.   perror_file (entry);
  1596.   /* NOTREACHED */
  1597. }
  1598.  
  1599. /* Print the filename of ENTRY on OUTFILE (a stdio stream),
  1600.    and then a newline.  */
  1601.  
  1602. void
  1603. prline_file_name (entry, outfile)
  1604.      struct file_entry *entry;
  1605.      FILE *outfile;
  1606. {
  1607.   print_file_name (entry, outfile);
  1608.   fprintf (outfile, "\n");
  1609. }
  1610.  
  1611. /* Print the filename of ENTRY on OUTFILE (a stdio stream).  */
  1612.  
  1613. void
  1614. print_file_name (entry, outfile)
  1615.      struct file_entry *entry;
  1616.      FILE *outfile;
  1617. {
  1618.   if (entry->superfile)
  1619.     {
  1620.       print_file_name (entry->superfile, outfile);
  1621.       fprintf (outfile, "(%s)", entry->filename);
  1622.     }
  1623.   else
  1624.     fprintf (outfile, "%s", entry->filename);
  1625. }
  1626.  
  1627. /* Return the filename of entry as a string (malloc'd for the purpose) */
  1628.  
  1629. char *
  1630. get_file_name (entry)
  1631.      struct file_entry *entry;
  1632. {
  1633.   char *result, *supfile;
  1634.   if (entry->superfile)
  1635.     {
  1636.       supfile = get_file_name (entry->superfile);
  1637.       result = (char *) xmalloc (strlen (supfile)
  1638.                  + strlen (entry->filename) + 3);
  1639.       sprintf (result, "%s(%s)", supfile, entry->filename);
  1640.       free (supfile);
  1641.     }
  1642.   else
  1643.     {
  1644.       result = (char *) xmalloc (strlen (entry->filename) + 1);
  1645.       strcpy (result, entry->filename);
  1646.     }
  1647.   return result;
  1648. }
  1649.  
  1650. /* Medium-level input routines for rel files.  */
  1651.  
  1652. /* Read a file's header into the proper place in the file_entry.
  1653.    DESC is the descriptor on which the file is open.
  1654.    ENTRY is the file's entry.  */
  1655.  
  1656. void
  1657. read_header (desc, entry)
  1658.      int desc;
  1659.      register struct file_entry *entry;
  1660. {
  1661.   register int len;
  1662.   struct exec *loc = (struct exec *) &entry->header;
  1663.  
  1664.   lseek (desc, entry->starting_offset, 0);
  1665. #ifdef COFF_ENCAPSULATE
  1666.   if (entry->just_syms_flag)
  1667.     lseek (desc, sizeof(coffheader), 1);
  1668. #endif
  1669.   len = read (desc, loc, sizeof (struct exec));
  1670.   if (len != sizeof (struct exec))
  1671.     fatal_with_file ("failure reading header of ", entry);
  1672.   if (N_BADMAG (*loc))
  1673.     fatal_with_file ("bad magic number in ", entry);
  1674.  
  1675.   entry->header_read_flag = 1;
  1676. }
  1677.  
  1678. /* Read the symbols of file ENTRY into core.
  1679.    Assume it is already open, on descriptor DESC.
  1680.    Also read the length of the string table, which follows the symbol table,
  1681.    but don't read the contents of the string table.  */
  1682.  
  1683. void
  1684. read_entry_symbols (desc, entry)
  1685.      struct file_entry *entry;
  1686.      int desc;
  1687. {
  1688.   int str_size;
  1689.  
  1690.   if (!entry->header_read_flag)
  1691.     read_header (desc, entry);
  1692.  
  1693.   entry->symbols = (struct nlist *) xmalloc (entry->header.a_syms);
  1694.  
  1695.   lseek (desc, N_SYMOFF (entry->header) + entry->starting_offset, 0);
  1696.   if (entry->header.a_syms != read (desc, entry->symbols, entry->header.a_syms))
  1697.     fatal_with_file ("premature end of file in symbols of ", entry);
  1698.  
  1699.   lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  1700.   if (sizeof str_size != read (desc, &str_size, sizeof str_size))
  1701.     fatal_with_file ("bad string table size in ", entry);
  1702.  
  1703.   entry->string_size = str_size;
  1704. }
  1705.  
  1706. /* Read the string table of file ENTRY into core.
  1707.    Assume it is already open, on descriptor DESC.
  1708.    Also record whether a GDB symbol segment follows the string table.  */
  1709.  
  1710. void
  1711. read_entry_strings (desc, entry)
  1712.      struct file_entry *entry;
  1713.      int desc;
  1714. {
  1715.   int buffer;
  1716.  
  1717.   if (!entry->header_read_flag)
  1718.     read_header (desc, entry);
  1719.  
  1720.   lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  1721.   if (entry->string_size != read (desc, entry->strings, entry->string_size))
  1722.     fatal_with_file ("premature end of file in strings of ", entry);
  1723.  
  1724.   /* While we are here, see if the file has a symbol segment at the end.
  1725.      For a separate file, just try reading some more.
  1726.      For a library member, compare current pos against total size.  */
  1727.   if (entry->superfile)
  1728.     {
  1729.       if (entry->total_size == N_STROFF (entry->header) + entry->string_size)
  1730.     return;
  1731.     }
  1732.   else
  1733.     {
  1734.       buffer = read (desc, &buffer, sizeof buffer);
  1735.       if (buffer == 0)
  1736.     return;
  1737.       if (buffer != sizeof buffer)
  1738.     fatal_with_file ("premature end of file in GDB symbol segment of ", entry);
  1739.     }
  1740.   /* Don't try to do anything with symsegs.  */
  1741.   return;
  1742. #if 0
  1743.   /* eliminate warning of `statement not reached'.  */
  1744.   entry->symseg_offset = N_STROFF (entry->header) + entry->string_size;
  1745. #endif
  1746. }
  1747.  
  1748. /* Read in the symbols of all input files.  */
  1749.  
  1750. void read_file_symbols (), read_entry_symbols (), read_entry_strings ();
  1751. void enter_file_symbols (), enter_global_ref (), search_library ();
  1752.  
  1753. void
  1754. load_symbols ()
  1755. {
  1756.   register int i;
  1757.  
  1758.   if (trace_files) fprintf (stderr, "Loading symbols:\n\n");
  1759.  
  1760.   for (i = 0; i < number_of_files; i++)
  1761.     {
  1762.       register struct file_entry *entry = &file_table[i];
  1763.       read_file_symbols (entry);
  1764.     }
  1765.  
  1766.   if (trace_files) fprintf (stderr, "\n");
  1767. }
  1768.  
  1769. /* If ENTRY is a rel file, read its symbol and string sections into core.
  1770.    If it is a library, search it and load the appropriate members
  1771.    (which means calling this function recursively on those members).  */
  1772.  
  1773. void
  1774. read_file_symbols (entry)
  1775.      register struct file_entry *entry;
  1776. {
  1777.   register int desc;
  1778.   register int len;
  1779.   struct exec hdr;
  1780.  
  1781.   desc = file_open (entry);
  1782.  
  1783. #ifdef COFF_ENCAPSULATE
  1784.   if (entry->just_syms_flag)
  1785.     lseek (desc, sizeof(coffheader),0);
  1786. #endif
  1787.  
  1788.   len = read (desc, &hdr, sizeof hdr);
  1789.   if (len != sizeof hdr)
  1790.     fatal_with_file ("failure reading header of ", entry);
  1791.  
  1792.   if (!N_BADMAG (hdr))
  1793.     {
  1794.       read_entry_symbols (desc, entry);
  1795.       entry->strings = (char *) alloca (entry->string_size);
  1796.       read_entry_strings (desc, entry);
  1797.       enter_file_symbols (entry);
  1798.       entry->strings = 0;
  1799.     }
  1800.   else
  1801.     {
  1802.       char armag[SARMAG];
  1803.  
  1804.       lseek (desc, 0, 0);
  1805.       if (SARMAG != read (desc, armag, SARMAG) || strncmp (armag, ARMAG, SARMAG))
  1806.     fatal_with_file ("malformed input file (not rel or archive) ", entry);
  1807.       entry->library_flag = 1;
  1808.       search_library (desc, entry);
  1809.     }
  1810.  
  1811.   file_close ();
  1812. }
  1813.  
  1814. /* Enter the external symbol defs and refs of ENTRY in the hash table.  */
  1815.  
  1816. void
  1817. enter_file_symbols (entry)
  1818.      struct file_entry *entry;
  1819. {
  1820.   register struct nlist
  1821.     *p,
  1822.     *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  1823.  
  1824.   if (trace_files) prline_file_name (entry, stderr);
  1825.  
  1826.   for (p = entry->symbols; p < end; p++)
  1827.     {
  1828.       if (p->n_type == (N_SETV | N_EXT)) continue;
  1829.       if (set_element_prefixes
  1830.       && set_element_prefixed_p (p->n_un.n_strx + entry->strings))
  1831.     p->n_type += (N_SETA - N_ABS);
  1832.  
  1833.       if (SET_ELEMENT_P (p->n_type))
  1834.     {
  1835.       set_symbol_count++;
  1836.       if (!relocatable_output)
  1837.         enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1838.     }
  1839.       else if (p->n_type == N_WARNING)
  1840.     {
  1841.       char *name = p->n_un.n_strx + entry->strings;
  1842.  
  1843.       /* Grab the next entry.  */
  1844.       p++;
  1845.       if (p->n_type != (N_UNDF | N_EXT))
  1846.         {
  1847.           fprintf (stderr, "%s: Warning symbol found in %s without external reference following.\n",
  1848.                progname, entry->filename);
  1849.           make_executable = 0;
  1850.           p--;        /* Process normally.  */
  1851.         }
  1852.       else
  1853.         {
  1854.           symbol *sp;
  1855.           char *sname = p->n_un.n_strx + entry->strings;
  1856.           /* Deal with the warning symbol.  */
  1857.           enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1858.           sp = getsym (sname);
  1859.           sp->warning = (char *) xmalloc (strlen(name) + 1);
  1860.           strcpy (sp->warning, name);
  1861.           warning_count++;
  1862.         }
  1863.     }
  1864.       else if (p->n_type & N_EXT)
  1865.     enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1866.       else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT)))
  1867.     {
  1868.       if ((p->n_un.n_strx + entry->strings)[0] != LPREFIX)
  1869.         non_L_local_sym_count++;
  1870.       local_sym_count++;
  1871.     }
  1872.       else debugger_sym_count++;
  1873.     }
  1874.  
  1875.    /* Count one for the local symbol that we generate,
  1876.       whose name is the file's name (usually) and whose address
  1877.       is the start of the file's text.  */
  1878.  
  1879.   local_sym_count++;
  1880.   non_L_local_sym_count++;
  1881. }
  1882.  
  1883. /* Enter one global symbol in the hash table.
  1884.    NLIST_P points to the `struct nlist' read from the file
  1885.    that describes the global symbol.  NAME is the symbol's name.
  1886.    ENTRY is the file entry for the file the symbol comes from.
  1887.  
  1888.    The `struct nlist' is modified by placing it on a chain of
  1889.    all such structs that refer to the same global symbol.
  1890.    This chain starts in the `refs' field of the symbol table entry
  1891.    and is chained through the `n_name'.  */
  1892.  
  1893. void
  1894. enter_global_ref (nlist_p, name, entry)
  1895.      register struct nlist *nlist_p;
  1896.      char *name;
  1897.      struct file_entry *entry;
  1898. {
  1899.   register symbol *sp = getsym (name);
  1900.   register int type = nlist_p->n_type;
  1901.   int oldref = sp->referenced;
  1902.   int olddef = sp->defined;
  1903.  
  1904.   nlist_p->n_un.n_name = (char *) sp->refs;
  1905.   sp->refs = nlist_p;
  1906.  
  1907.   sp->referenced = 1;
  1908.   if (type != (N_UNDF | N_EXT) || nlist_p->n_value)
  1909.     {
  1910.       if (!sp->defined || sp->defined == (N_UNDF | N_EXT))
  1911.     sp->defined = type;
  1912.  
  1913.       if (oldref && !olddef)
  1914.     /* It used to be undefined and we're defining it.  */
  1915.     undefined_global_sym_count--;
  1916.  
  1917.       if (!olddef && type == (N_UNDF | N_EXT) && nlist_p->n_value)
  1918.     {
  1919.       /* First definition and it's common.  */
  1920.       common_defined_global_count++;
  1921.       sp->max_common_size = nlist_p->n_value;
  1922.     }
  1923.       else if (olddef && sp->max_common_size && type != (N_UNDF | N_EXT))
  1924.     {
  1925.       /* It used to be common and we're defining it as
  1926.          something else.  */
  1927.       common_defined_global_count--;
  1928.       sp->max_common_size = 0;
  1929.     }
  1930.       else if (olddef && sp->max_common_size && type == (N_UNDF | N_EXT)
  1931.       && sp->max_common_size < nlist_p->n_value)
  1932.     /* It used to be common and this is a new common entry to
  1933.        which we need to pay attention.  */
  1934.     sp->max_common_size = nlist_p->n_value;
  1935.  
  1936.       /* Are we defining it as a set element?  */
  1937.       if (SET_ELEMENT_P (type)
  1938.       && (!olddef || (olddef && sp->max_common_size)))
  1939.     set_vector_count++;
  1940.       /* As an indirection?  */
  1941.       else if (type == (N_INDR | N_EXT))
  1942.     {
  1943.       /* Indirect symbols value should be modified to point
  1944.          a symbol being equivalenced to. */
  1945.       nlist_p->n_value
  1946.         = (unsigned int) getsym ((nlist_p + 1)->n_un.n_strx
  1947.                      + entry->strings);
  1948.       if ((symbol *) nlist_p->n_value == sp)
  1949.         {
  1950.           /* Somebody redefined a symbol to be itself.  */
  1951.           fprintf (stderr, "%s: Symbol %s indirected to itself.\n",
  1952.                entry->filename, name);
  1953.           /* Rewrite this symbol as being a global text symbol
  1954.          with value 0.  */
  1955.           nlist_p->n_type = sp->defined = N_TEXT | N_EXT;
  1956.           nlist_p->n_value = 0;
  1957.           /* Don't make the output executable.  */
  1958.           make_executable = 0;
  1959.         }
  1960.       else
  1961.         global_indirect_count++;
  1962.     }
  1963.     }
  1964.   else
  1965.     if (!oldref)
  1966. #ifndef DOLLAR_KLUDGE
  1967.       undefined_global_sym_count++;
  1968. #else
  1969.       {
  1970.     if (entry->superfile && type == (N_UNDF | N_EXT) && name[1] == '$')
  1971.       {
  1972.         /* This is an (ISI?) $-conditional; skip it */
  1973.         sp->referenced = 0;
  1974.         if (sp->trace)
  1975.           {
  1976.         fprintf (stderr, "symbol %s is a $-conditional ignored in ", sp->name);
  1977.         print_file_name (entry, stderr);
  1978.         fprintf (stderr, "\n");
  1979.           }
  1980.         return;
  1981.       }
  1982.     else
  1983.       undefined_global_sym_count++;
  1984.       }
  1985. #endif
  1986.  
  1987.   if (sp == end_symbol && entry->just_syms_flag && !T_flag_specified)
  1988.     text_start = nlist_p->n_value;
  1989.  
  1990.   if (sp->trace)
  1991.     {
  1992.       register char *reftype;
  1993.       switch (type & N_TYPE)
  1994.     {
  1995.     case N_UNDF:
  1996.       if (nlist_p->n_value)
  1997.         reftype = "defined as common";
  1998.       else reftype = "referenced";
  1999.       break;
  2000.  
  2001.     case N_ABS:
  2002.       reftype = "defined as absolute";
  2003.       break;
  2004.  
  2005.     case N_TEXT:
  2006.       reftype = "defined in text section";
  2007.       break;
  2008.  
  2009.     case N_DATA:
  2010.       reftype = "defined in data section";
  2011.       break;
  2012.  
  2013.     case N_BSS:
  2014.       reftype = "defined in BSS section";
  2015.       break;
  2016.  
  2017.     case N_SETT:
  2018.       reftype = "is a text set element";
  2019.       break;
  2020.  
  2021.     case N_SETD:
  2022.       reftype = "is a data set element";
  2023.       break;
  2024.  
  2025.     case N_SETB:
  2026.       reftype = "is a BSS set element";
  2027.       break;
  2028.  
  2029.     case N_SETA:
  2030.       reftype = "is an absolute set element";
  2031.       break;
  2032.  
  2033.     case N_SETV:
  2034.       reftype = "defined in data section as vector";
  2035.       break;
  2036.  
  2037.     case N_INDR:
  2038.       reftype = (char *) alloca (23
  2039.                      + strlen ((nlist_p + 1)->n_un.n_strx
  2040.                            + entry->strings));
  2041.       sprintf (reftype, "defined equivalent to %s",
  2042.            (nlist_p + 1)->n_un.n_strx + entry->strings);
  2043.       break;
  2044.  
  2045. #ifdef sequent
  2046.     case N_SHUNDF:
  2047.       reftype = "shared undf";
  2048.       break;
  2049.  
  2050. /* These conflict with cases above.
  2051.     case N_SHDATA:
  2052.       reftype = "shared data";
  2053.       break;
  2054.  
  2055.     case N_SHBSS:
  2056.       reftype = "shared BSS";
  2057.       break;
  2058. */
  2059.     default:
  2060.       reftype = "I don't know this type";
  2061.       break;
  2062. #endif
  2063.     }
  2064.  
  2065.       fprintf (stderr, "symbol %s %s in ", sp->name, reftype);
  2066.       print_file_name (entry, stderr);
  2067.       fprintf (stderr, "\n");
  2068.     }
  2069. }
  2070.  
  2071. /* This return 0 if the given file entry's symbol table does *not*
  2072.    contain the nlist point entry, and it returns the files entry
  2073.    pointer (cast to unsigned long) if it does. */
  2074.  
  2075. unsigned long
  2076. contains_symbol (entry, n_ptr)
  2077.      struct file_entry *entry;
  2078.      register struct nlist *n_ptr;
  2079. {
  2080.   if (n_ptr >= entry->symbols &&
  2081.       n_ptr < (entry->symbols
  2082.            + (entry->header.a_syms / sizeof (struct nlist))))
  2083.     return (unsigned long) entry;
  2084.   return 0;
  2085. }
  2086.  
  2087.  
  2088. /* Searching libraries */
  2089.  
  2090. struct file_entry *decode_library_subfile ();
  2091. void linear_library (), symdef_library ();
  2092.  
  2093. /* Search the library ENTRY, already open on descriptor DESC.
  2094.    This means deciding which library members to load,
  2095.    making a chain of `struct file_entry' for those members,
  2096.    and entering their global symbols in the hash table.  */
  2097.  
  2098. void
  2099. search_library (desc, entry)
  2100.      int desc;
  2101.      struct file_entry *entry;
  2102. {
  2103.   int member_length;
  2104.   register char *name;
  2105.   register struct file_entry *subentry;
  2106.  
  2107.   if (!undefined_global_sym_count) return;
  2108.  
  2109.   /* Examine its first member, which starts SARMAG bytes in.  */
  2110.   subentry = decode_library_subfile (desc, entry, SARMAG, &member_length);
  2111.   if (!subentry) return;
  2112.  
  2113.   name = subentry->filename;
  2114.   free (subentry);
  2115.  
  2116.   /* Search via __.SYMDEF if that exists, else linearly.  */
  2117.  
  2118.   if (!strcmp (name, "__.SYMDEF"))
  2119.     symdef_library (desc, entry, member_length);
  2120.   else
  2121.     linear_library (desc, entry);
  2122. }
  2123.  
  2124. /* Construct and return a file_entry for a library member.
  2125.    The library's file_entry is library_entry, and the library is open on DESC.
  2126.    SUBFILE_OFFSET is the byte index in the library of this member's header.
  2127.    We store the length of the member into *LENGTH_LOC.  */
  2128.  
  2129. struct file_entry *
  2130. decode_library_subfile (desc, library_entry, subfile_offset, length_loc)
  2131.      int desc;
  2132.      struct file_entry *library_entry;
  2133.      int subfile_offset;
  2134.      int *length_loc;
  2135. {
  2136.   int bytes_read;
  2137.   register int namelen;
  2138.   int member_length;
  2139.   register char *name;
  2140.   struct ar_hdr hdr1;
  2141.   register struct file_entry *subentry;
  2142.  
  2143.   lseek (desc, subfile_offset, 0);
  2144.  
  2145.   bytes_read = read (desc, &hdr1, sizeof hdr1);
  2146.   if (!bytes_read)
  2147.     return 0;        /* end of archive */
  2148.  
  2149.   if (sizeof hdr1 != bytes_read)
  2150.     fatal_with_file ("malformed library archive ", library_entry);
  2151.  
  2152.   if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
  2153.     fatal_with_file ("malformatted header of archive member in ", library_entry);
  2154.  
  2155.   subentry = (struct file_entry *) xmalloc (sizeof (struct file_entry));
  2156.   bzero (subentry, sizeof (struct file_entry));
  2157.  
  2158.   for (namelen = 0;
  2159.        namelen < sizeof hdr1.ar_name
  2160.        && hdr1.ar_name[namelen] != 0 && hdr1.ar_name[namelen] != ' '
  2161. #ifdef GNUDOS
  2162.     ;
  2163. #else
  2164.        && hdr1.ar_name[namelen] != '/';
  2165. #endif
  2166.        namelen++);
  2167.  
  2168.   name = (char *) xmalloc (namelen+1);
  2169.   strncpy (name, hdr1.ar_name, namelen);
  2170.   name[namelen] = 0;
  2171.  
  2172.   subentry->filename = name;
  2173.   subentry->local_sym_name = name;
  2174.   subentry->symbols = 0;
  2175.   subentry->strings = 0;
  2176.   subentry->subfiles = 0;
  2177.   subentry->starting_offset = subfile_offset + sizeof hdr1;
  2178.   subentry->superfile = library_entry;
  2179.   subentry->library_flag = 0;
  2180.   subentry->header_read_flag = 0;
  2181.   subentry->just_syms_flag = 0;
  2182.   subentry->chain = 0;
  2183.   subentry->total_size = member_length;
  2184.  
  2185.   (*length_loc) = member_length;
  2186.  
  2187.   return subentry;
  2188. }
  2189.  
  2190. int subfile_wanted_p ();
  2191.  
  2192. /* Search a library that has a __.SYMDEF member.
  2193.    DESC is a descriptor on which the library is open.
  2194.      The file pointer is assumed to point at the __.SYMDEF data.
  2195.    ENTRY is the library's file_entry.
  2196.    MEMBER_LENGTH is the length of the __.SYMDEF data.  */
  2197.  
  2198. void
  2199. symdef_library (desc, entry, member_length)
  2200.      int desc;
  2201.      struct file_entry *entry;
  2202.      int member_length;
  2203. {
  2204.   int *symdef_data = (int *) xmalloc (member_length);
  2205.   register struct symdef *symdef_base;
  2206.   char *sym_name_base;
  2207.   int number_of_symdefs;
  2208.   int length_of_strings;
  2209.   int not_finished;
  2210.   int bytes_read;
  2211.   register int i;
  2212.   struct file_entry *prev = 0;
  2213.   int prev_offset = 0;
  2214.  
  2215.   bytes_read = read (desc, symdef_data, member_length);
  2216.   if (bytes_read != member_length)
  2217.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2218.  
  2219.   number_of_symdefs = *symdef_data / sizeof (struct symdef);
  2220.   if (number_of_symdefs < 0 ||
  2221.        number_of_symdefs * sizeof (struct symdef) + 2 * sizeof (int) > member_length)
  2222.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2223.  
  2224.   symdef_base = (struct symdef *) (symdef_data + 1);
  2225.   length_of_strings = *(int *) (symdef_base + number_of_symdefs);
  2226.  
  2227.   if (length_of_strings < 0
  2228.       || number_of_symdefs * sizeof (struct symdef) + length_of_strings
  2229.       + 2 * sizeof (int) != member_length)
  2230.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2231.  
  2232.   sym_name_base = sizeof (int) + (char *) (symdef_base + number_of_symdefs);
  2233.  
  2234.   /* Check all the string indexes for validity.  */
  2235.  
  2236.   for (i = 0; i < number_of_symdefs; i++)
  2237.     {
  2238.       register int index = symdef_base[i].symbol_name_string_index;
  2239.       if (index < 0 || index >= length_of_strings
  2240.       || (index && *(sym_name_base + index - 1)))
  2241.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2242.     }
  2243.  
  2244.   /* Search the symdef data for members to load.
  2245.      Do this until one whole pass finds nothing to load.  */
  2246.  
  2247.   not_finished = 1;
  2248.   while (not_finished)
  2249.     {
  2250.       not_finished = 0;
  2251.  
  2252.       /* Scan all the symbols mentioned in the symdef for ones that we need.
  2253.      Load the library members that contain such symbols.  */
  2254.  
  2255.       for (i = 0;
  2256.        (i < number_of_symdefs
  2257.         && (undefined_global_sym_count || common_defined_global_count));
  2258.        i++)
  2259.     if (symdef_base[i].symbol_name_string_index >= 0)
  2260.       {
  2261.         register symbol *sp;
  2262.  
  2263.         sp = getsym_soft (sym_name_base
  2264.                   + symdef_base[i].symbol_name_string_index);
  2265.  
  2266.         /* If we find a symbol that appears to be needed, think carefully
  2267.            about the archive member that the symbol is in.  */
  2268.  
  2269.         if (sp && ((sp->referenced && !sp->defined)
  2270.                || (sp->defined && sp->max_common_size)))
  2271.           {
  2272.         int junk;
  2273.         register int j;
  2274.         register int offset = symdef_base[i].library_member_offset;
  2275.         struct file_entry *subentry;
  2276.  
  2277.         /* Don't think carefully about any archive member
  2278.            more than once in a given pass.  */
  2279.  
  2280.         if (prev_offset == offset)
  2281.           continue;
  2282.         prev_offset = offset;
  2283.  
  2284.         /* Read the symbol table of the archive member.  */
  2285.  
  2286.         subentry = decode_library_subfile (desc, entry, offset, &junk);
  2287.         if (subentry == 0)
  2288.           fatal ("invalid offset for %s in symbol table of %s",
  2289.              sym_name_base
  2290.              + symdef_base[i].symbol_name_string_index,
  2291.              entry->filename);
  2292.         read_entry_symbols (desc, subentry);
  2293.         subentry->strings = (char *) malloc (subentry->string_size);
  2294.         read_entry_strings (desc, subentry);
  2295.  
  2296.         /* Now scan the symbol table and decide whether to load.  */
  2297.  
  2298.         if (!subfile_wanted_p (subentry))
  2299.           {
  2300.             free (subentry->symbols);
  2301.             free (subentry);
  2302.           }
  2303.         else
  2304.           {
  2305.             /* This member is needed; load it.
  2306.                Since we are loading something on this pass,
  2307.                we must make another pass through the symdef data.  */
  2308.  
  2309.             not_finished = 1;
  2310.  
  2311.             enter_file_symbols (subentry);
  2312.  
  2313.             if (prev)
  2314.               prev->chain = subentry;
  2315.             else entry->subfiles = subentry;
  2316.             prev = subentry;
  2317.  
  2318.             /* Clear out this member's symbols from the symdef data
  2319.                so that following passes won't waste time on them.  */
  2320.  
  2321.             for (j = 0; j < number_of_symdefs; j++)
  2322.               {
  2323.             if (symdef_base[j].library_member_offset == offset)
  2324.               symdef_base[j].symbol_name_string_index = -1;
  2325.               }
  2326.           }
  2327.  
  2328.         /* We'll read the strings again if we need them again.  */
  2329.         free (subentry->strings);
  2330.         subentry->strings = 0;
  2331.           }
  2332.       }
  2333.     }
  2334.  
  2335.   free (symdef_data);
  2336. }
  2337.  
  2338. /* Search a library that has no __.SYMDEF.
  2339.    ENTRY is the library's file_entry.
  2340.    DESC is the descriptor it is open on.  */
  2341.  
  2342. void
  2343. linear_library (desc, entry)
  2344.      int desc;
  2345.      struct file_entry *entry;
  2346. {
  2347.   register struct file_entry *prev = 0;
  2348.   register int this_subfile_offset = SARMAG;
  2349.  
  2350.   while (undefined_global_sym_count || common_defined_global_count)
  2351.     {
  2352.       int member_length;
  2353.       register struct file_entry *subentry;
  2354.  
  2355.       subentry = decode_library_subfile (desc, entry, this_subfile_offset,
  2356.                      &member_length);
  2357.  
  2358.       if (!subentry) return;
  2359.  
  2360.       read_entry_symbols (desc, subentry);
  2361.       subentry->strings = (char *) alloca (subentry->string_size);
  2362.       read_entry_strings (desc, subentry);
  2363.  
  2364.       if (!subfile_wanted_p (subentry))
  2365.     {
  2366.       free (subentry->symbols);
  2367.       free (subentry);
  2368.     }
  2369.       else
  2370.     {
  2371.       enter_file_symbols (subentry);
  2372.  
  2373.       if (prev)
  2374.         prev->chain = subentry;
  2375.       else entry->subfiles = subentry;
  2376.       prev = subentry;
  2377.       subentry->strings = 0; /* Since space will dissapear on return */
  2378.     }
  2379.  
  2380.       this_subfile_offset += member_length + sizeof (struct ar_hdr);
  2381.       if (this_subfile_offset & 1) this_subfile_offset++;
  2382.     }
  2383. }
  2384.  
  2385. /* ENTRY is an entry for a library member.
  2386.    Its symbols have been read into core, but not entered.
  2387.    Return nonzero if we ought to load this member.  */
  2388.  
  2389. int
  2390. subfile_wanted_p (entry)
  2391.      struct file_entry *entry;
  2392. {
  2393.   register struct nlist *p;
  2394.   register struct nlist *end
  2395.     = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2396. #ifdef DOLLAR_KLUDGE
  2397.   register int dollar_cond = 0;
  2398. #endif
  2399.  
  2400.   for (p = entry->symbols; p < end; p++)
  2401.     {
  2402.       register int type = p->n_type;
  2403.       register char *name = p->n_un.n_strx + entry->strings;
  2404.  
  2405.       /* If the symbol has an interesting definition, we could
  2406.      potentially want it.  */
  2407.       if (type & N_EXT
  2408.       && (type != (N_UNDF | N_EXT) || p->n_value
  2409.  
  2410. #ifdef DOLLAR_KLUDGE
  2411.            || name[1] == '$'
  2412. #endif
  2413.           )
  2414.       && !SET_ELEMENT_P (type)
  2415.       && !set_element_prefixed_p (name))
  2416.     {
  2417.       register symbol *sp = getsym_soft (name);
  2418.  
  2419. #ifdef DOLLAR_KLUDGE
  2420.       if (name[1] == '$')
  2421.         {
  2422.           sp = getsym_soft (&name[2]);
  2423.           dollar_cond = 1;
  2424.           if (!sp) continue;
  2425.           if (sp->referenced)
  2426.         {
  2427.           if (write_map)
  2428.             {
  2429.               print_file_name (entry, stdout);
  2430.               fprintf (stdout, " needed due to $-conditional %s\n", name);
  2431.             }
  2432.           return 1;
  2433.         }
  2434.           continue;
  2435.         }
  2436. #endif
  2437.  
  2438.       /* If this symbol has not been hashed, we can't be looking for it. */
  2439.  
  2440.       if (!sp) continue;
  2441.  
  2442.       if ((sp->referenced && !sp->defined)
  2443.           || (sp->defined && sp->max_common_size))
  2444.         {
  2445.           /* This is a symbol we are looking for.  It is either
  2446.              not yet defined or defined as a common.  */
  2447. #ifdef DOLLAR_KLUDGE
  2448.           if (dollar_cond) continue;
  2449. #endif
  2450.           if (type == (N_UNDF | N_EXT))
  2451.         {
  2452.           /* Symbol being defined as common.
  2453.              Remember this, but don't load subfile just for this.  */
  2454.  
  2455.           /* If it didn't used to be common, up the count of
  2456.              common symbols.  */
  2457.           if (!sp->max_common_size)
  2458.             common_defined_global_count++;
  2459.  
  2460.           if (sp->max_common_size < p->n_value)
  2461.             sp->max_common_size = p->n_value;
  2462.           if (!sp->defined)
  2463.             undefined_global_sym_count--;
  2464.           sp->defined = 1;
  2465.           continue;
  2466.         }
  2467.  
  2468.           if (write_map)
  2469.         {
  2470.           print_file_name (entry, stdout);
  2471.           fprintf (stdout, " needed due to %s\n", sp->name);
  2472.         }
  2473.           return 1;
  2474.         }
  2475.     }
  2476.     }
  2477.  
  2478.   return 0;
  2479. }
  2480.  
  2481. void consider_file_section_lengths (), relocate_file_addresses ();
  2482.  
  2483. /* Having entered all the global symbols and found the sizes of sections
  2484.    of all files to be linked, make all appropriate deductions from this data.
  2485.  
  2486.    We propagate global symbol values from definitions to references.
  2487.    We compute the layout of the output file and where each input file's
  2488.    contents fit into it.  */
  2489.  
  2490. void
  2491. digest_symbols ()
  2492. {
  2493.   register int i;
  2494.   int setv_fill_count;
  2495.  
  2496.   if (trace_files)
  2497.     fprintf (stderr, "Digesting symbol information:\n\n");
  2498.  
  2499.   /* Compute total size of sections */
  2500.  
  2501.   each_file (consider_file_section_lengths, 0);
  2502.  
  2503.   /* If necessary, pad text section to full page in the file.
  2504.      Include the padding in the text segment size.  */
  2505.  
  2506. #ifdef NMAGIC
  2507.   if (magic == ZMAGIC || magic == NMAGIC)
  2508. #else
  2509.   if (magic == ZMAGIC)
  2510. #endif
  2511.     {
  2512.       int text_end = text_size + N_TXTOFF (outheader);
  2513.       text_pad = ((text_end + page_size - 1) & (- page_size)) - text_end;
  2514.       text_size += text_pad;
  2515.     }
  2516.  
  2517. #ifdef _N_BASEADDR
  2518.   /* SunOS 4.1 N_TXTADDR depends on the value of outheader.a_entry.  */
  2519.   outheader.a_entry = N_PAGSIZ (outheader);
  2520. #endif
  2521.  
  2522.   outheader.a_text = text_size;
  2523. #ifdef sequent
  2524.   outheader.a_text += N_ADDRADJ (outheader);
  2525. #endif
  2526.  
  2527.   /* Make the data segment address start in memory on a suitable boundary.  */
  2528.  
  2529.   if (! Tdata_flag_specified)
  2530.     data_start = N_DATADDR (outheader) + text_start - TEXT_START (outheader);
  2531.  
  2532.   /* Set up the set element vector */
  2533.  
  2534.   if (!relocatable_output)
  2535.     {
  2536.       /* The set sector size is the number of set elements + a word
  2537.          for each symbol for the length word at the beginning of the
  2538.      vector, plus a word for each symbol for a zero at the end of
  2539.      the vector (for incremental linking).  */
  2540.       set_sect_size
  2541.     = (2 * set_symbol_count + set_vector_count) * sizeof (unsigned long);
  2542.       set_sect_start = data_start + data_size;
  2543.       data_size += set_sect_size;
  2544.       set_vectors = (unsigned long *) xmalloc (set_sect_size);
  2545.       setv_fill_count = 0;
  2546.     }
  2547.  
  2548.   /* Compute start addresses of each file's sections and symbols.  */
  2549.  
  2550.   each_full_file (relocate_file_addresses, 0);
  2551.  
  2552.   /* Now, for each symbol, verify that it is defined globally at most once.
  2553.      Put the global value into the symbol entry.
  2554.      Common symbols are allocated here, in the BSS section.
  2555.      Each defined symbol is given a '->defined' field
  2556.       which is the correct N_ code for its definition,
  2557.       except in the case of common symbols with -r.
  2558.      Then make all the references point at the symbol entry
  2559.      instead of being chained together. */
  2560.  
  2561.   defined_global_sym_count = 0;
  2562.  
  2563.   for (i = 0; i < TABSIZE; i++)
  2564.     {
  2565.       register symbol *sp;
  2566.       for (sp = symtab[i]; sp; sp = sp->link)
  2567.     {
  2568.       /* For each symbol */
  2569.       register struct nlist *p, *next;
  2570.       int defs = 0, com = sp->max_common_size;
  2571.       struct nlist *first_definition;
  2572.       for (p = sp->refs; p; p = next)
  2573.         {
  2574.           register int type = p->n_type;
  2575.  
  2576.           if (SET_ELEMENT_P (type))
  2577.         {
  2578.           if (relocatable_output)
  2579.             fatal ("internal: global ref to set element with -r");
  2580.           if (!defs++)
  2581.             {
  2582.               sp->value = set_sect_start
  2583.             + setv_fill_count++ * sizeof (unsigned long);
  2584.               sp->defined = N_SETV | N_EXT;
  2585.               first_definition = p;
  2586.             }
  2587.           else if ((sp->defined & ~N_EXT) != N_SETV)
  2588.             {
  2589.               sp->multiply_defined = 1;
  2590.               multiple_def_count++;
  2591.             }
  2592.           set_vectors[setv_fill_count++] = p->n_value;
  2593.         }
  2594.           else if ((type & N_EXT) && type != (N_UNDF | N_EXT))
  2595.         {
  2596.           /* non-common definition */
  2597.           if (defs++ && sp->value != p->n_value)
  2598.             {
  2599.               sp->multiply_defined = 1;
  2600.               multiple_def_count++;
  2601.             }
  2602.           sp->value = p->n_value;
  2603.           sp->defined = type;
  2604.           first_definition = p;
  2605.         }
  2606.           next = (struct nlist *) p->n_un.n_name;
  2607.           p->n_un.n_name = (char *) sp;
  2608.         }
  2609.       /* Allocate as common if defined as common and not defined for real */
  2610.       if (com && !defs)
  2611.         {
  2612.           if (!relocatable_output || force_common_definition)
  2613.         {
  2614.           int align = sizeof (int);
  2615.  
  2616.           /* Round up to nearest sizeof (int).  I don't know
  2617.              whether this is necessary or not (given that
  2618.              alignment is taken care of later), but it's
  2619.              traditional, so I'll leave it in.  Note that if
  2620.              this size alignment is ever removed, ALIGN above
  2621.              will have to be initialized to 1 instead of
  2622.              sizeof (int).  */
  2623.  
  2624.           com = (com + sizeof (int) - 1) & (- sizeof (int));
  2625.  
  2626.           while (!(com & align))
  2627.             align <<= 1;
  2628.  
  2629.           align = align > MAX_ALIGNMENT ? MAX_ALIGNMENT : align;
  2630.  
  2631.           bss_size = ((((bss_size + data_size + data_start)
  2632.                   + (align - 1)) & (- align))
  2633.                   - data_size - data_start);
  2634.  
  2635.           sp->value = data_start + data_size + bss_size;
  2636.           sp->defined = N_BSS | N_EXT;
  2637.           bss_size += com;
  2638.           if (write_map)
  2639.             printf ("Allocating common %s: %x at %x\n",
  2640.                 sp->name, com, sp->value);
  2641.         }
  2642.           else
  2643.         {
  2644.           sp->defined = 0;
  2645.           undefined_global_sym_count++;
  2646.         }
  2647.         }
  2648.       /* Set length word at front of vector and zero byte at end.
  2649.          Reverse the vector itself to put it in file order.  */
  2650.       if ((sp->defined & ~N_EXT) == N_SETV)
  2651.         {
  2652.           unsigned long length_word_index
  2653.         = (sp->value - set_sect_start) / sizeof (unsigned long);
  2654.           unsigned long i, tmp;
  2655.  
  2656.           set_vectors[length_word_index]
  2657.         = setv_fill_count - 1 - length_word_index;
  2658.  
  2659.           /* Reverse the vector.  */
  2660.           for (i = 1;
  2661.            i < (setv_fill_count - length_word_index - 1) / 2 + 1;
  2662.            i++)
  2663.         {
  2664.           tmp = set_vectors[length_word_index + i];
  2665.           set_vectors[length_word_index + i]
  2666.             = set_vectors[setv_fill_count - i];
  2667.           set_vectors[setv_fill_count - i] = tmp;
  2668.         }
  2669.  
  2670.           set_vectors[setv_fill_count++] = 0;
  2671.         }
  2672.       if (sp->defined)
  2673.         defined_global_sym_count++;
  2674.     }
  2675.     }
  2676.  
  2677.   if (end_symbol)        /* These are null if -r.  */
  2678.     {
  2679.       etext_symbol->value = text_size + text_start;
  2680.       edata_symbol->value = data_start + data_size;
  2681.       end_symbol->value = data_start + data_size + bss_size;
  2682.     }
  2683.  
  2684.   /* Figure the data_pad now, so that it overlaps with the bss addresses.  */
  2685.  
  2686.   if (specified_data_size && specified_data_size > data_size)
  2687.     data_pad = specified_data_size - data_size;
  2688.  
  2689.   if (magic == ZMAGIC)
  2690.     data_pad = ((data_pad + data_size + page_size - 1) & (- page_size))
  2691.                - data_size;
  2692.  
  2693.   bss_size -= data_pad;
  2694.   if (bss_size < 0) bss_size = 0;
  2695.  
  2696.   data_size += data_pad;
  2697. }
  2698.  
  2699. /* Accumulate the section sizes of input file ENTRY
  2700.    into the section sizes of the output file.  */
  2701.  
  2702. void
  2703. consider_file_section_lengths (entry)
  2704.      register struct file_entry *entry;
  2705. {
  2706.   if (entry->just_syms_flag)
  2707.     return;
  2708.  
  2709.   entry->text_start_address = text_size;
  2710.   /* If there were any vectors, we need to chop them off */
  2711.   text_size += entry->header.a_text;
  2712.   entry->data_start_address = data_size;
  2713.   data_size += entry->header.a_data;
  2714.   entry->bss_start_address = bss_size;
  2715.   bss_size += entry->header.a_bss;
  2716.  
  2717.   text_reloc_size += entry->header.a_trsize;
  2718.   data_reloc_size += entry->header.a_drsize;
  2719. }
  2720.  
  2721. /* Determine where the sections of ENTRY go into the output file,
  2722.    whose total section sizes are already known.
  2723.    Also relocate the addresses of the file's local and debugger symbols.  */
  2724.  
  2725. void
  2726. relocate_file_addresses (entry)
  2727.      register struct file_entry *entry;
  2728. {
  2729.   entry->text_start_address += text_start;
  2730.   /* Note that `data_start' and `data_size' have not yet been
  2731.      adjusted for `data_pad'.  If they had been, we would get the wrong
  2732.      results here.  */
  2733.   entry->data_start_address += data_start;
  2734.   entry->bss_start_address += data_start + data_size;
  2735.  
  2736.   {
  2737.     register struct nlist *p;
  2738.     register struct nlist *end
  2739.       = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2740.  
  2741.     for (p = entry->symbols; p < end; p++)
  2742.       {
  2743.     /* If this belongs to a section, update it by the section's start address */
  2744.     register int type = p->n_type & N_TYPE;
  2745.  
  2746.     switch (type)
  2747.       {
  2748.       case N_TEXT:
  2749.       case N_SETT:
  2750.         p->n_value += entry->text_start_address;
  2751.         break;
  2752.       case N_DATA:
  2753.       case N_SETV:
  2754.       case N_SETD:
  2755.         /* A symbol whose value is in the data section
  2756.            is present in the input file as if the data section
  2757.            started at an address equal to the length of the file's text.  */
  2758.         p->n_value += entry->data_start_address - entry->header.a_text;
  2759.         break;
  2760.       case N_BSS:
  2761.       case N_SETB:
  2762.         /* likewise for symbols with value in BSS.  */
  2763.         p->n_value += entry->bss_start_address
  2764.           - entry->header.a_text - entry->header.a_data;
  2765.         break;
  2766.       }
  2767.       }
  2768.   }
  2769. }
  2770.  
  2771. void describe_file_sections (), list_file_locals ();
  2772.  
  2773. /* Print a complete or partial map of the output file.  */
  2774.  
  2775. void
  2776. print_symbols (outfile)
  2777.      FILE *outfile;
  2778. {
  2779.   register int i;
  2780.  
  2781.   fprintf (outfile, "\nFiles:\n\n");
  2782.  
  2783.   each_file (describe_file_sections, outfile);
  2784.  
  2785.   fprintf (outfile, "\nGlobal symbols:\n\n");
  2786.  
  2787.   for (i = 0; i < TABSIZE; i++)
  2788.     {
  2789.       register symbol *sp;
  2790.       for (sp = symtab[i]; sp; sp = sp->link)
  2791.     {
  2792.       if (sp->defined == 1)
  2793.         fprintf (outfile, "  %s: common, length 0x%x\n", sp->name, sp->max_common_size);
  2794.       if (sp->defined)
  2795.         fprintf (outfile, "  %s: 0x%x\n", sp->name, sp->value);
  2796.       else if (sp->referenced)
  2797.         fprintf (outfile, "  %s: undefined\n", sp->name);
  2798.     }
  2799.     }
  2800.  
  2801.   each_file (list_file_locals, outfile);
  2802. }
  2803.  
  2804. void
  2805. describe_file_sections (entry, outfile)
  2806.      struct file_entry *entry;
  2807.      FILE *outfile;
  2808. {
  2809.   fprintf (outfile, "  ");
  2810.   print_file_name (entry, outfile);
  2811.   if (entry->just_syms_flag)
  2812.     fprintf (outfile, " symbols only\n", 0);
  2813.   else
  2814.     fprintf (outfile, " text %x(%x), data %x(%x), bss %x(%x) hex\n",
  2815.          entry->text_start_address, entry->header.a_text,
  2816.          entry->data_start_address, entry->header.a_data,
  2817.          entry->bss_start_address, entry->header.a_bss);
  2818. }
  2819.  
  2820. void
  2821. list_file_locals (entry, outfile)
  2822.      struct file_entry *entry;
  2823.      FILE *outfile;
  2824. {
  2825.   register struct nlist
  2826.     *p,
  2827.     *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2828.  
  2829.   entry->strings = (char *) alloca (entry->string_size);
  2830.   read_entry_strings (file_open (entry), entry);
  2831.  
  2832.   fprintf (outfile, "\nLocal symbols of ");
  2833.   print_file_name (entry, outfile);
  2834.   fprintf (outfile, ":\n\n");
  2835.  
  2836.   for (p = entry->symbols; p < end; p++)
  2837.     /* If this is a definition,
  2838.        update it if necessary by this file's start address.  */
  2839.     if (!(p->n_type & (N_STAB | N_EXT)))
  2840.       fprintf (outfile, "  %s: 0x%x\n",
  2841.            entry->strings + p->n_un.n_strx, p->n_value);
  2842.  
  2843.   entry->strings = 0;        /* All done with them.  */
  2844. }
  2845.  
  2846.  
  2847. /* Static vars for do_warnings and subroutines of it */
  2848. int list_unresolved_refs;    /* List unresolved refs */
  2849. int list_warning_symbols;    /* List warning syms */
  2850. int list_multiple_defs;        /* List multiple definitions */
  2851.  
  2852. /*
  2853.  * Structure for communication between do_file_warnings and it's
  2854.  * helper routines.  Will in practice be an array of three of these:
  2855.  * 0) Current line, 1) Next line, 2) Source file info.
  2856.  */
  2857. struct line_debug_entry
  2858. {
  2859.   int line;
  2860.   char *filename;
  2861.   struct nlist *sym;
  2862. };
  2863.  
  2864. void qsort ();
  2865. /*
  2866.  * Helper routines for do_file_warnings.
  2867.  */
  2868.  
  2869. /* Return an integer less than, equal to, or greater than 0 as per the
  2870.    relation between the two relocation entries.  Used by qsort.  */
  2871.  
  2872. int
  2873. relocation_entries_relation (rel1, rel2)
  2874.      struct relocation_info *rel1, *rel2;
  2875. {
  2876.   return RELOC_ADDRESS(rel1) - RELOC_ADDRESS(rel2);
  2877. }
  2878.  
  2879. /* Moves to the next debugging symbol in the file.  USE_DATA_SYMBOLS
  2880.    determines the type of the debugging symbol to look for (DSLINE or
  2881.    SLINE).  STATE_POINTER keeps track of the old and new locatiosn in
  2882.    the file.  It assumes that state_pointer[1] is valid; ie
  2883.    that it.sym points into some entry in the symbol table.  If
  2884.    state_pointer[1].sym == 0, this routine should not be called.  */
  2885.  
  2886. int
  2887. next_debug_entry (use_data_symbols, state_pointer)
  2888.      register int use_data_symbols;
  2889.      /* Next must be passed by reference! */
  2890.      struct line_debug_entry state_pointer[3];
  2891. {
  2892.   register struct line_debug_entry
  2893.     *current = state_pointer,
  2894.     *next = state_pointer + 1,
  2895.     /* Used to store source file */
  2896.     *source = state_pointer + 2;
  2897.   struct file_entry *entry = (struct file_entry *) source->sym;
  2898.  
  2899.   current->sym = next->sym;
  2900.   current->line = next->line;
  2901.   current->filename = next->filename;
  2902.  
  2903.   while (++(next->sym) < (entry->symbols
  2904.               + entry->header.a_syms/sizeof (struct nlist)))
  2905.     {
  2906.       /* n_type is a char, and N_SOL, N_EINCL and N_BINCL are > 0x80, so
  2907.        * may look negative...therefore, must mask to low bits
  2908.        */
  2909.       switch (next->sym->n_type & 0xff)
  2910.     {
  2911.     case N_SLINE:
  2912.       if (use_data_symbols) continue;
  2913.       next->line = next->sym->n_desc;
  2914.       return 1;
  2915.     case N_DSLINE:
  2916.       if (!use_data_symbols) continue;
  2917.       next->line = next->sym->n_desc;
  2918.       return 1;
  2919. #ifdef HAVE_SUN_STABS
  2920.     case N_EINCL:
  2921.       next->filename = source->filename;
  2922.       continue;
  2923. #endif
  2924.     case N_SO:
  2925.       source->filename = next->sym->n_un.n_strx + entry->strings;
  2926.       source->line++;
  2927. #ifdef HAVE_SUN_STABS
  2928.     case N_BINCL:
  2929. #endif
  2930.     case N_SOL:
  2931.       next->filename
  2932.         = next->sym->n_un.n_strx + entry->strings;
  2933.     default:
  2934.       continue;
  2935.     }
  2936.     }
  2937.   next->sym = (struct nlist *) 0;
  2938.   return 0;
  2939. }
  2940.  
  2941. /* Create a structure to save the state of a scan through the debug
  2942.    symbols.  USE_DATA_SYMBOLS is set if we should be scanning for
  2943.    DSLINE's instead of SLINE's.  entry is the file entry which points
  2944.    at the symbols to use.  */
  2945.  
  2946. struct line_debug_entry *
  2947. init_debug_scan (use_data_symbols, entry)
  2948.      int use_data_symbols;
  2949.      struct file_entry *entry;
  2950. {
  2951.   struct line_debug_entry
  2952.     *state_pointer
  2953.       = (struct line_debug_entry *)
  2954.     xmalloc (3 * sizeof (struct line_debug_entry));
  2955.   register struct line_debug_entry
  2956.     *current = state_pointer,
  2957.     *next = state_pointer + 1,
  2958.     *source = state_pointer + 2; /* Used to store source file */
  2959.  
  2960.   struct nlist *tmp;
  2961.  
  2962.   for (tmp = entry->symbols;
  2963.        tmp < (entry->symbols
  2964.           + entry->header.a_syms/sizeof (struct nlist));
  2965.        tmp++)
  2966.     if (tmp->n_type == (int) N_SO)
  2967.       break;
  2968.  
  2969.   if (tmp >= (entry->symbols
  2970.           + entry->header.a_syms/sizeof (struct nlist)))
  2971.     {
  2972.       /* I believe this translates to "We lose" */
  2973.       current->filename = next->filename = entry->filename;
  2974.       current->line = next->line = -1;
  2975.       current->sym = next->sym = (struct nlist *) 0;
  2976.       return state_pointer;
  2977.     }
  2978.  
  2979.   next->line = source->line = 0;
  2980.   next->filename = source->filename
  2981.     = (tmp->n_un.n_strx + entry->strings);
  2982.   source->sym = (struct nlist *) entry;
  2983.   next->sym = tmp;
  2984.  
  2985.   next_debug_entry (use_data_symbols, state_pointer); /* To setup next */
  2986.  
  2987.   if (!next->sym)        /* No line numbers for this section; */
  2988.                 /* setup output results as appropriate */
  2989.     {
  2990.       if (source->line)
  2991.     {
  2992.       current->filename = source->filename = entry->filename;
  2993.       current->line = -1;    /* Don't print lineno */
  2994.     }
  2995.       else
  2996.     {
  2997.       current->filename = source->filename;
  2998.       current->line = 0;
  2999.     }
  3000.       return state_pointer;
  3001.     }
  3002.  
  3003.  
  3004.   next_debug_entry (use_data_symbols, state_pointer); /* To setup current */
  3005.  
  3006.   return state_pointer;
  3007. }
  3008.  
  3009. /* Takes an ADDRESS (in either text or data space) and a STATE_POINTER
  3010.    which describes the current location in the implied scan through
  3011.    the debug symbols within the file which ADDRESS is within, and
  3012.    returns the source line number which corresponds to ADDRESS.  */
  3013.  
  3014. int
  3015. address_to_line (address, state_pointer)
  3016.      unsigned long address;
  3017.      /* Next must be passed by reference! */
  3018.      struct line_debug_entry state_pointer[3];
  3019. {
  3020.   struct line_debug_entry
  3021.     *current = state_pointer,
  3022.     *next = state_pointer + 1;
  3023.   struct line_debug_entry *tmp_pointer;
  3024.  
  3025.   int use_data_symbols;
  3026.  
  3027.   if (next->sym)
  3028.     use_data_symbols = (next->sym->n_type & N_TYPE) == N_DATA;
  3029.   else
  3030.     return current->line;
  3031.  
  3032.   /* Go back to the beginning if we've already passed it.  */
  3033.   if (current->sym->n_value > address)
  3034.     {
  3035.       tmp_pointer = init_debug_scan (use_data_symbols,
  3036.                      (struct file_entry *)
  3037.                      ((state_pointer + 2)->sym));
  3038.       state_pointer[0] = tmp_pointer[0];
  3039.       state_pointer[1] = tmp_pointer[1];
  3040.       state_pointer[2] = tmp_pointer[2];
  3041.       free (tmp_pointer);
  3042.     }
  3043.  
  3044.   /* If we're still in a bad way, return -1, meaning invalid line.  */
  3045.   if (current->sym->n_value > address)
  3046.     return -1;
  3047.  
  3048.   while (next->sym
  3049.      && next->sym->n_value <= address
  3050.      && next_debug_entry (use_data_symbols, state_pointer))
  3051.     ;
  3052.   return current->line;
  3053. }
  3054.  
  3055.  
  3056. /* Macros for manipulating bitvectors.  */
  3057. #define    BIT_SET_P(bv, index)    ((bv)[(index) >> 3] & 1 << ((index) & 0x7))
  3058. #define    SET_BIT(bv, index)    ((bv)[(index) >> 3] |= 1 << ((index) & 0x7))
  3059.  
  3060. /* This routine will scan through the relocation data of file ENTRY,
  3061.    printing out references to undefined symbols and references to
  3062.    symbols defined in files with N_WARNING symbols.  If DATA_SEGMENT
  3063.    is non-zero, it will scan the data relocation segment (and use
  3064.    N_DSLINE symbols to track line number); otherwise it will scan the
  3065.    text relocation segment.  Warnings will be printed on the output
  3066.    stream OUTFILE.  Eventually, every nlist symbol mapped through will
  3067.    be marked in the NLIST_BITVECTOR, so we don't repeat ourselves when
  3068.    we scan the nlists themselves.  */
  3069.  
  3070. do_relocation_warnings (entry, data_segment, outfile, nlist_bitvector)
  3071.      struct file_entry *entry;
  3072.      int data_segment;
  3073.      FILE *outfile;
  3074.      unsigned char *nlist_bitvector;
  3075. {
  3076.   struct relocation_info
  3077.     *reloc_start = data_segment ? entry->datarel : entry->textrel,
  3078.     *reloc;
  3079.   int reloc_size
  3080.     = ((data_segment ? entry->header.a_drsize : entry->header.a_trsize)
  3081.        / sizeof (struct relocation_info));
  3082.   int start_of_segment
  3083.     = (data_segment ? entry->data_start_address : entry->text_start_address);
  3084.   struct nlist *start_of_syms = entry->symbols;
  3085.   struct line_debug_entry *state_pointer
  3086.     = init_debug_scan (data_segment != 0, entry);
  3087.   register struct line_debug_entry
  3088.     *current = state_pointer;
  3089.   /* Assigned to generally static values; should not be written into.  */
  3090.   char *errfmt;
  3091.   /* Assigned to alloca'd values cand copied into; should be freed
  3092.      when done.  */
  3093.   char *errmsg;
  3094.   int invalidate_line_number;
  3095.  
  3096.   /* We need to sort the relocation info here.  Sheesh, so much effort
  3097.      for one lousy error optimization. */
  3098.  
  3099.   qsort (reloc_start, reloc_size, sizeof (struct relocation_info),
  3100.      relocation_entries_relation);
  3101.  
  3102.   for (reloc = reloc_start;
  3103.        reloc < (reloc_start + reloc_size);
  3104.        reloc++)
  3105.     {
  3106.       register struct nlist *s;
  3107.       register symbol *g;
  3108.  
  3109.       /* If the relocation isn't resolved through a symbol, continue */
  3110.       if (!RELOC_EXTERN_P(reloc))
  3111.     continue;
  3112.  
  3113.       s = &(entry->symbols[RELOC_SYMBOL(reloc)]);
  3114.  
  3115.       /* Local symbols shouldn't ever be used by relocation info, so
  3116.      the next should be safe.
  3117.      This is, of course, wrong.  References to local BSS symbols can be
  3118.      the targets of relocation info, and they can (must) be
  3119.      resolved through symbols.  However, these must be defined properly,
  3120.      (the assembler would have caught it otherwise), so we can
  3121.      ignore these cases.  */
  3122.       if (!(s->n_type & N_EXT))
  3123.     continue;
  3124.  
  3125.       g = (symbol *) s->n_un.n_name;
  3126.       errmsg = 0;
  3127.  
  3128.       if (!g->defined && list_unresolved_refs) /* Reference */
  3129.     {
  3130.       /* Mark as being noted by relocation warning pass.  */
  3131.       SET_BIT (nlist_bitvector, s - start_of_syms);
  3132.  
  3133.       if (g->undef_refs >= MAX_UREFS_PRINTED)    /* Listed too many */
  3134.         continue;
  3135.  
  3136.       /* Undefined symbol which we should mention */
  3137.  
  3138.       if (++(g->undef_refs) == MAX_UREFS_PRINTED)
  3139.         {
  3140.           errfmt = "More undefined symbol %s refs follow";
  3141.           invalidate_line_number = 1;
  3142.         }
  3143.       else
  3144.         {
  3145.           errfmt = "Undefined symbol %s referenced from %s segment";
  3146.           invalidate_line_number = 0;
  3147.         }
  3148.     }
  3149.       else                         /* Defined */
  3150.     {
  3151.       /* Potential symbol warning here */
  3152.       if (!g->warning) continue;
  3153.  
  3154.       /* Mark as being noted by relocation warning pass.  */
  3155.       SET_BIT (nlist_bitvector, s - start_of_syms);
  3156.  
  3157.       errfmt = 0;
  3158.       errmsg = g->warning;
  3159.       invalidate_line_number = 0;
  3160.     }
  3161.  
  3162.  
  3163.       /* If errfmt == 0, errmsg has already been defined.  */
  3164.       if (errfmt != 0)
  3165.     {
  3166.       char *nm;
  3167.  
  3168.       if (demangler == NULL || (nm = (*demangler)(g->name)) == NULL)
  3169.         nm = g->name;
  3170.       errmsg = (char *) xmalloc (strlen (errfmt) + strlen (nm) + 1);
  3171.       sprintf (errmsg, errfmt, nm, data_segment ? "data" : "text");
  3172.       if (nm != g->name)
  3173.         free (nm);
  3174.     }
  3175.  
  3176.       address_to_line (RELOC_ADDRESS (reloc) + start_of_segment,
  3177.                state_pointer);
  3178.  
  3179.       if (current->line >=0)
  3180.     fprintf (outfile, "%s:%d: %s\n", current->filename,
  3181.          invalidate_line_number ? 0 : current->line, errmsg);
  3182.       else
  3183.     fprintf (outfile, "%s: %s\n", current->filename, errmsg);
  3184.  
  3185.       if (errfmt != 0)
  3186.     free (errmsg);
  3187.     }
  3188.  
  3189.   free (state_pointer);
  3190. }
  3191.  
  3192. /* Print on OUTFILE a list of all warnings generated by references
  3193.    and/or definitions in the file ENTRY.  List source file and line
  3194.    number if possible, just the .o file if not. */
  3195.  
  3196. void
  3197. do_file_warnings (entry, outfile)
  3198.      struct file_entry *entry;
  3199.      FILE *outfile;
  3200. {
  3201.   int number_of_syms = entry->header.a_syms / sizeof (struct nlist);
  3202.   unsigned char *nlist_bitvector
  3203.     = (unsigned char *) alloca ((number_of_syms >> 3) + 1);
  3204.   struct line_debug_entry *text_scan, *data_scan;
  3205.   int i;
  3206.   char *errfmt, *file_name;
  3207.   int line_number;
  3208.   int dont_allow_symbol_name;
  3209.  
  3210.   bzero (nlist_bitvector, (number_of_syms >> 3) + 1);
  3211.  
  3212.   /* Read in the files strings if they aren't available */
  3213.   if (!entry->strings)
  3214.     {
  3215.       int desc;
  3216.  
  3217.       entry->strings = (char *) alloca (entry->string_size);
  3218.       desc = file_open (entry);
  3219.       read_entry_strings (desc, entry);
  3220.     }
  3221.  
  3222.   read_file_relocation (entry);
  3223.  
  3224.   /* Do text warnings based on a scan through the relocation info.  */
  3225.   do_relocation_warnings (entry, 0, outfile, nlist_bitvector);
  3226.  
  3227.   /* Do data warnings based on a scan through the relocation info.  */
  3228.   do_relocation_warnings (entry, 1, outfile, nlist_bitvector);
  3229.  
  3230.   /* Scan through all of the nlist entries in this file and pick up
  3231.      anything that the scan through the relocation stuff didn't.  */
  3232.  
  3233.   text_scan = init_debug_scan (0, entry);
  3234.   data_scan = init_debug_scan (1, entry);
  3235.  
  3236.   for (i = 0; i < number_of_syms; i++)
  3237.     {
  3238.       struct nlist *s;
  3239.       struct glosym *g;
  3240.  
  3241.       s = entry->symbols + i;
  3242.  
  3243.       if (!(s->n_type & N_EXT))
  3244.     continue;
  3245.  
  3246.       g = (symbol *) s->n_un.n_name;
  3247.       dont_allow_symbol_name = 0;
  3248.  
  3249.       if (list_multiple_defs && g->multiply_defined)
  3250.     {
  3251.       errfmt = "Definition of symbol %s (multiply defined)";
  3252.       switch (s->n_type)
  3253.         {
  3254.         case N_TEXT | N_EXT:
  3255.           line_number = address_to_line (s->n_value, text_scan);
  3256.           file_name = text_scan[0].filename;
  3257.           break;
  3258.         case N_DATA | N_EXT:
  3259.           line_number = address_to_line (s->n_value, data_scan);
  3260.           file_name = data_scan[0].filename;
  3261.           break;
  3262.         case N_SETA | N_EXT:
  3263.         case N_SETT | N_EXT:
  3264.         case N_SETD | N_EXT:
  3265.         case N_SETB | N_EXT:
  3266.           if (g->multiply_defined == 2)
  3267.         continue;
  3268.           errfmt = "First set element definition of symbol %s (multiply defined)";
  3269.           break;
  3270.         default:
  3271.           continue;        /* Don't print out multiple defs
  3272.                    at references.  */
  3273.         }
  3274.     }
  3275.       else if (BIT_SET_P (nlist_bitvector, i))
  3276.     continue;
  3277.       else if (list_unresolved_refs && !g->defined)
  3278.     {
  3279.       if (g->undef_refs >= MAX_UREFS_PRINTED)
  3280.         continue;
  3281.  
  3282.       if (++(g->undef_refs) == MAX_UREFS_PRINTED)
  3283.         errfmt = "More undefined \"%s\" refs follow";
  3284.       else
  3285.         errfmt = "Undefined symbol \"%s\" referenced";
  3286.       line_number = -1;
  3287.     }
  3288.       else if (g->warning)
  3289.     {
  3290.       /* There are two cases in which we don't want to
  3291.          do this.  The first is if this is a definition instead of
  3292.          a reference.  The second is if it's the reference used by
  3293.          the warning stabs itself.  */
  3294.       if (s->n_type != (N_EXT | N_UNDF)
  3295.           || (i && (s-1)->n_type == N_WARNING))
  3296.         continue;
  3297.  
  3298.       errfmt = g->warning;
  3299.       line_number = -1;
  3300.       dont_allow_symbol_name = 1;
  3301.     }
  3302.       else
  3303.     continue;
  3304.  
  3305.       if (line_number == -1)
  3306.     fprintf (outfile, "%s: ", entry->filename);
  3307.       else
  3308.     fprintf (outfile, "%s:%d: ", file_name, line_number);
  3309.  
  3310.       if (dont_allow_symbol_name)
  3311.     fprintf (outfile, "%s", errfmt);
  3312.       else
  3313.     {
  3314.       char *nm;
  3315.       if (demangler != NULL && (nm = (*demangler)(g->name)) != NULL)
  3316.         {
  3317.           fprintf (outfile, errfmt, nm);
  3318.           free (nm);
  3319.         }
  3320.       else
  3321.         fprintf (outfile, errfmt, g->name);
  3322.     }
  3323.  
  3324.       fputc ('\n', outfile);
  3325.     }
  3326.   free (text_scan);
  3327.   free (data_scan);
  3328.   entry->strings = 0;        /* Since it will dissapear anyway.  */
  3329. }
  3330.  
  3331. do_warnings (outfile)
  3332.      FILE *outfile;
  3333. {
  3334.   list_unresolved_refs = !relocatable_output && undefined_global_sym_count;
  3335.   list_warning_symbols = warning_count;
  3336.   list_multiple_defs = multiple_def_count != 0;
  3337.  
  3338.   if (!(list_unresolved_refs ||
  3339.     list_warning_symbols ||
  3340.     list_multiple_defs      ))
  3341.     /* No need to run this routine */
  3342.     return;
  3343.  
  3344.   each_file (do_file_warnings, outfile);
  3345.  
  3346.   if (list_unresolved_refs || list_multiple_defs)
  3347.     make_executable = 0;
  3348. }
  3349.  
  3350. /* Write the output file */
  3351.  
  3352. void
  3353. write_output ()
  3354. {
  3355.   struct stat statbuf;
  3356.   int filemode;
  3357.  
  3358.   (void) unlink (output_filename);
  3359.   outdesc = open (output_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  3360.   if (outdesc < 0) perror_name (output_filename);
  3361.  
  3362.   if (fstat (outdesc, &statbuf) < 0)
  3363.     perror_name (output_filename);
  3364.  
  3365.   filemode = statbuf.st_mode;
  3366.  
  3367. /*  chmod (output_filename, filemode & ~0111); */
  3368.  
  3369.   /* Output the a.out header.  */
  3370.   write_header ();
  3371.  
  3372.   /* Output the text and data segments, relocating as we go.  */
  3373.   write_text ();
  3374.   write_data ();
  3375.  
  3376.   /* Output the merged relocation info, if requested with `-r'.  */
  3377.   if (relocatable_output)
  3378.     write_rel ();
  3379.  
  3380.   /* Output the symbol table (both globals and locals).  */
  3381.   write_syms ();
  3382.  
  3383.   /* Copy any GDB symbol segments from input files.  */
  3384.   write_symsegs ();
  3385.  
  3386.   close (outdesc);
  3387.  
  3388. /*  if (chmod (output_filename, filemode | 0111) == -1)
  3389.     perror_name (output_filename); */
  3390. }
  3391.  
  3392. void modify_location (), perform_relocation (), copy_text (), copy_data ();
  3393.  
  3394. void
  3395. write_header ()
  3396. {
  3397.   N_SET_MAGIC (outheader, magic);
  3398.   outheader.a_text = text_size;
  3399. #ifdef sequent
  3400.   outheader.a_text += N_ADDRADJ (outheader);
  3401.   if (entry_symbol == 0)
  3402.     entry_symbol = getsym("start");
  3403. #endif
  3404. #ifdef GNUDOS
  3405.   if (entry_symbol == 0)
  3406.     entry_symbol = getsym("start");
  3407. #endif
  3408.   outheader.a_data = data_size;
  3409.   outheader.a_bss = bss_size;
  3410.   outheader.a_entry = (entry_symbol ? entry_symbol->value
  3411.                : text_start + entry_offset);
  3412. #ifdef COFF_ENCAPSULATE
  3413.   if (need_coff_header)
  3414.     {
  3415.       /* We are encapsulating BSD format within COFF format.  */
  3416.       struct coffscn *tp, *dp, *bp;
  3417.  
  3418.       tp = &coffheader.scns[0];
  3419.       dp = &coffheader.scns[1];
  3420.       bp = &coffheader.scns[2];
  3421.  
  3422.       strcpy (tp->s_name, ".text");
  3423.       tp->s_paddr = text_start;
  3424.       tp->s_vaddr = text_start;
  3425.       tp->s_size = text_size;
  3426.       tp->s_scnptr = sizeof (struct coffheader) + sizeof (struct exec);
  3427.       tp->s_relptr = 0;
  3428.       tp->s_lnnoptr = 0;
  3429.       tp->s_nreloc = 0;
  3430.       tp->s_nlnno = 0;
  3431.       tp->s_flags = 0x20;
  3432.       strcpy (dp->s_name, ".data");
  3433.       dp->s_paddr = data_start;
  3434.       dp->s_vaddr = data_start;
  3435.       dp->s_size = data_size;
  3436.       dp->s_scnptr = tp->s_scnptr + tp->s_size;
  3437.       dp->s_relptr = 0;
  3438.       dp->s_lnnoptr = 0;
  3439.       dp->s_nreloc = 0;
  3440.       dp->s_nlnno = 0;
  3441.       dp->s_flags = 0x40;
  3442.       strcpy (bp->s_name, ".bss");
  3443.       bp->s_paddr = dp->s_vaddr + dp->s_size;
  3444.       bp->s_vaddr = bp->s_paddr;
  3445.       bp->s_size = bss_size;
  3446.       bp->s_scnptr = 0;
  3447.       bp->s_relptr = 0;
  3448.       bp->s_lnnoptr = 0;
  3449.       bp->s_nreloc = 0;
  3450.       bp->s_nlnno = 0;
  3451.       bp->s_flags = 0x80;
  3452.  
  3453.       coffheader.f_magic = COFF_MAGIC;
  3454.       coffheader.f_nscns = 3;
  3455.       /* store an unlikely time so programs can
  3456.        * tell that there is a bsd header
  3457.        */
  3458.       coffheader.f_timdat = 1;
  3459.       coffheader.f_symptr = 0;
  3460.       coffheader.f_nsyms = 0;
  3461.       coffheader.f_opthdr = 28;
  3462.       coffheader.f_flags = 0x103;
  3463.       /* aouthdr */
  3464.       coffheader.magic = ZMAGIC;
  3465.       coffheader.vstamp = 0;
  3466.       coffheader.tsize = tp->s_size;
  3467.       coffheader.dsize = dp->s_size;
  3468.       coffheader.bsize = bp->s_size;
  3469.       coffheader.entry = outheader.a_entry;
  3470.       coffheader.text_start = tp->s_vaddr;
  3471.       coffheader.data_start = dp->s_vaddr;
  3472.     }
  3473. #endif
  3474.  
  3475. #ifdef INITIALIZE_HEADER
  3476.   INITIALIZE_HEADER;
  3477. #endif
  3478.  
  3479.   if (strip_symbols == STRIP_ALL)
  3480.     nsyms = 0;
  3481.   else
  3482.     {
  3483.       nsyms = (defined_global_sym_count
  3484.            + undefined_global_sym_count);
  3485.       if (discard_locals == DISCARD_L)
  3486.     nsyms += non_L_local_sym_count;
  3487.       else if (discard_locals == DISCARD_NONE)
  3488.     nsyms += local_sym_count;
  3489.       /* One extra for following reference on indirects */
  3490.       if (relocatable_output)
  3491.     nsyms += set_symbol_count + global_indirect_count;
  3492.     }
  3493.  
  3494.   if (strip_symbols == STRIP_NONE)
  3495.     nsyms += debugger_sym_count;
  3496.  
  3497.   outheader.a_syms = nsyms * sizeof (struct nlist);
  3498.  
  3499.   if (relocatable_output)
  3500.     {
  3501.       outheader.a_trsize = text_reloc_size;
  3502.       outheader.a_drsize = data_reloc_size;
  3503.     }
  3504.   else
  3505.     {
  3506.       outheader.a_trsize = 0;
  3507.       outheader.a_drsize = 0;
  3508.     }
  3509.  
  3510. #ifdef COFF_ENCAPSULATE
  3511.   if (need_coff_header)
  3512.     mywrite (&coffheader, sizeof coffheader, 1, outdesc);
  3513. #endif
  3514.   mywrite (&outheader, sizeof (struct exec), 1, outdesc);
  3515.  
  3516.   /* Output whatever padding is required in the executable file
  3517.      between the header and the start of the text.  */
  3518.  
  3519. #ifndef COFF_ENCAPSULATE
  3520.   padfile (N_TXTOFF (outheader) - sizeof outheader, outdesc);
  3521. #endif
  3522. }
  3523.  
  3524. /* Relocate the text segment of each input file
  3525.    and write to the output file.  */
  3526.  
  3527. void
  3528. write_text ()
  3529. {
  3530.   if (trace_files)
  3531.     fprintf (stderr, "Copying and relocating text:\n\n");
  3532.  
  3533.   each_full_file (copy_text, 0);
  3534.   file_close ();
  3535.  
  3536.   if (trace_files)
  3537.     fprintf (stderr, "\n");
  3538.  
  3539.   padfile (text_pad, outdesc);
  3540. }
  3541.  
  3542. int
  3543. text_offset (entry)
  3544.      struct file_entry *entry;
  3545. {
  3546.   return entry->starting_offset + N_TXTOFF (entry->header);
  3547. }
  3548.  
  3549. /* Read in all of the relocation information */
  3550.  
  3551. void
  3552. read_relocation ()
  3553. {
  3554.   each_full_file (read_file_relocation, 0);
  3555. }
  3556.  
  3557. /* Read in the relocation sections of ENTRY if necessary */
  3558.  
  3559. void
  3560. read_file_relocation (entry)
  3561.      struct file_entry *entry;
  3562. {
  3563.   register struct relocation_info *reloc;
  3564.   int desc;
  3565.   int read_return;
  3566.  
  3567.   desc = -1;
  3568.   if (!entry->textrel)
  3569.     {
  3570.       reloc = (struct relocation_info *) xmalloc (entry->header.a_trsize);
  3571.       desc = file_open (entry);
  3572.       lseek (desc,
  3573.          text_offset (entry) + entry->header.a_text + entry->header.a_data,
  3574.          L_SET);
  3575.       if (entry->header.a_trsize != (read_return = read (desc, reloc, entry->header.a_trsize)))
  3576.     {
  3577.       fprintf (stderr, "Return from read: %d\n", read_return);
  3578.       fatal_with_file ("premature eof in text relocation of ", entry);
  3579.     }
  3580.       entry->textrel = reloc;
  3581.     }
  3582.  
  3583.   if (!entry->datarel)
  3584.     {
  3585.       reloc = (struct relocation_info *) xmalloc (entry->header.a_drsize);
  3586.       if (desc == -1) desc = file_open (entry);
  3587.       lseek (desc,
  3588.          text_offset (entry) + entry->header.a_text
  3589.          + entry->header.a_data + entry->header.a_trsize,
  3590.          L_SET);
  3591.       if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
  3592.     fatal_with_file ("premature eof in data relocation of ", entry);
  3593.       entry->datarel = reloc;
  3594.     }
  3595. }
  3596.  
  3597. /* Read the text segment contents of ENTRY, relocate them,
  3598.    and write the result to the output file.
  3599.    If `-r', save the text relocation for later reuse.  */
  3600.  
  3601. void
  3602. copy_text (entry)
  3603.      struct file_entry *entry;
  3604. {
  3605.   register char *bytes;
  3606.   register int desc;
  3607.   register struct relocation_info *reloc;
  3608.  
  3609.   if (trace_files)
  3610.     prline_file_name (entry, stderr);
  3611.  
  3612.   desc = file_open (entry);
  3613.  
  3614.   /* Allocate space for the file's text section */
  3615.  
  3616.   bytes = (char *) alloca (entry->header.a_text);
  3617.  
  3618.   /* Deal with relocation information however is appropriate */
  3619.  
  3620.   if (entry->textrel)  reloc = entry->textrel;
  3621.   else if (relocatable_output)
  3622.     {
  3623.       read_file_relocation (entry);
  3624.       reloc = entry->textrel;
  3625.     }
  3626.   else
  3627.     {
  3628.       reloc = (struct relocation_info *) alloca (entry->header.a_trsize);
  3629.       lseek (desc, text_offset (entry) + entry->header.a_text + entry->header.a_data, 0);
  3630.       if (entry->header.a_trsize != read (desc, reloc, entry->header.a_trsize))
  3631.     fatal_with_file ("premature eof in text relocation of ", entry);
  3632.     }
  3633.  
  3634.   /* Read the text section into core.  */
  3635.  
  3636.   lseek (desc, text_offset (entry), 0);
  3637.   if (entry->header.a_text != read (desc, bytes, entry->header.a_text))
  3638.     fatal_with_file ("premature eof in text section of ", entry);
  3639.  
  3640.  
  3641.   /* Relocate the text according to the text relocation.  */
  3642.  
  3643.   perform_relocation (bytes, entry->text_start_address, entry->header.a_text,
  3644.               reloc, entry->header.a_trsize, entry);
  3645.  
  3646.   /* Write the relocated text to the output file.  */
  3647.  
  3648.   mywrite (bytes, 1, entry->header.a_text, outdesc);
  3649. }
  3650.  
  3651. /* Relocate the data segment of each input file
  3652.    and write to the output file.  */
  3653.  
  3654. void
  3655. write_data ()
  3656. {
  3657.   if (trace_files)
  3658.     fprintf (stderr, "Copying and relocating data:\n\n");
  3659.  
  3660.   each_full_file (copy_data, 0);
  3661.   file_close ();
  3662.  
  3663.   /* Write out the set element vectors.  See digest symbols for
  3664.      description of length of the set vector section.  */
  3665.  
  3666.   if (set_vector_count)
  3667.     mywrite (set_vectors, 2 * set_symbol_count + set_vector_count,
  3668.          sizeof (unsigned long), outdesc);
  3669.  
  3670.   if (trace_files)
  3671.     fprintf (stderr, "\n");
  3672.  
  3673.   padfile (data_pad, outdesc);
  3674. }
  3675.  
  3676. /* Read the data segment contents of ENTRY, relocate them,
  3677.    and write the result to the output file.
  3678.    If `-r', save the data relocation for later reuse.
  3679.    See comments in `copy_text'.  */
  3680.  
  3681. void
  3682. copy_data (entry)
  3683.      struct file_entry *entry;
  3684. {
  3685.   register struct relocation_info *reloc;
  3686.   register char *bytes;
  3687.   register int desc;
  3688.  
  3689.   if (trace_files)
  3690.     prline_file_name (entry, stderr);
  3691.  
  3692.   desc = file_open (entry);
  3693.  
  3694.   bytes = (char *) alloca (entry->header.a_data);
  3695.  
  3696.   if (entry->datarel) reloc = entry->datarel;
  3697.   else if (relocatable_output)    /* Will need this again */
  3698.     {
  3699.       read_file_relocation (entry);
  3700.       reloc = entry->datarel;
  3701.     }
  3702.   else
  3703.     {
  3704.       reloc = (struct relocation_info *) alloca (entry->header.a_drsize);
  3705.       lseek (desc, text_offset (entry) + entry->header.a_text
  3706.          + entry->header.a_data + entry->header.a_trsize,
  3707.          0);
  3708.       if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
  3709.     fatal_with_file ("premature eof in data relocation of ", entry);
  3710.     }
  3711.  
  3712.   lseek (desc, text_offset (entry) + entry->header.a_text, 0);
  3713.   if (entry->header.a_data != read (desc, bytes, entry->header.a_data))
  3714.     fatal_with_file ("premature eof in data section of ", entry);
  3715.  
  3716.   perform_relocation (bytes, entry->data_start_address - entry->header.a_text,
  3717.               entry->header.a_data, reloc, entry->header.a_drsize, entry);
  3718.  
  3719.   mywrite (bytes, 1, entry->header.a_data, outdesc);
  3720. }
  3721.  
  3722. /* Relocate ENTRY's text or data section contents.
  3723.    DATA is the address of the contents, in core.
  3724.    DATA_SIZE is the length of the contents.
  3725.    PC_RELOCATION is the difference between the address of the contents
  3726.      in the output file and its address in the input file.
  3727.    RELOC_INFO is the address of the relocation info, in core.
  3728.    RELOC_SIZE is its length in bytes.  */
  3729. /* This version is about to be severly hacked by Randy.  Hope it
  3730.    works afterwards. */
  3731. void
  3732. perform_relocation (data, pc_relocation, data_size, reloc_info, reloc_size, entry)
  3733.      char *data;
  3734.      struct relocation_info *reloc_info;
  3735.      struct file_entry *entry;
  3736.      int pc_relocation;
  3737.      int data_size;
  3738.      int reloc_size;
  3739. {
  3740.   register struct relocation_info *p = reloc_info;
  3741.   struct relocation_info *end
  3742.     = reloc_info + reloc_size / sizeof (struct relocation_info);
  3743.   int text_relocation = entry->text_start_address;
  3744.   int data_relocation = entry->data_start_address - entry->header.a_text;
  3745.   int bss_relocation
  3746.     = entry->bss_start_address - entry->header.a_text - entry->header.a_data;
  3747.  
  3748.   for (; p < end; p++)
  3749.     {
  3750.       register int relocation = 0;
  3751.       register int addr = RELOC_ADDRESS(p);
  3752.       register unsigned int mask = 0;
  3753.  
  3754.       if (addr >= data_size)
  3755.     fatal_with_file ("relocation address out of range in ", entry);
  3756.  
  3757.       if (RELOC_EXTERN_P(p))
  3758.     {
  3759.       int symindex = RELOC_SYMBOL (p) * sizeof (struct nlist);
  3760.       symbol *sp = ((symbol *)
  3761.             (((struct nlist *)
  3762.               (((char *)entry->symbols) + symindex))
  3763.              ->n_un.n_name));
  3764.  
  3765. #ifdef N_INDR
  3766.       /* Resolve indirection */
  3767.       if ((sp->defined & ~N_EXT) == N_INDR)
  3768.         sp = (symbol *) sp->value;
  3769. #endif
  3770.  
  3771.       if (symindex >= entry->header.a_syms)
  3772.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  3773.  
  3774.       /* If the symbol is undefined, leave it at zero.  */
  3775.       if (! sp->defined)
  3776.         relocation = 0;
  3777.       else
  3778.         relocation = sp->value;
  3779.     }
  3780.       else switch (RELOC_TYPE(p))
  3781.     {
  3782.     case N_TEXT:
  3783.     case N_TEXT | N_EXT:
  3784.       relocation = text_relocation;
  3785.       break;
  3786.  
  3787.     case N_DATA:
  3788.     case N_DATA | N_EXT:
  3789.       /* A word that points to beginning of the the data section
  3790.          initially contains not 0 but rather the "address" of that section
  3791.          in the input file, which is the length of the file's text.  */
  3792.       relocation = data_relocation;
  3793.       break;
  3794.  
  3795.     case N_BSS:
  3796.     case N_BSS | N_EXT:
  3797.       /* Similarly, an input word pointing to the beginning of the bss
  3798.          initially contains the length of text plus data of the file.  */
  3799.       relocation = bss_relocation;
  3800.       break;
  3801.  
  3802.     case N_ABS:
  3803.     case N_ABS | N_EXT:
  3804.       /* Don't know why this code would occur, but apparently it does.  */
  3805.       break;
  3806.  
  3807.     default:
  3808.       fatal_with_file ("nonexternal relocation code invalid in ", entry);
  3809.     }
  3810.  
  3811. #ifdef RELOC_ADD_EXTRA
  3812.       relocation += RELOC_ADD_EXTRA(p);
  3813.       if (relocatable_output)
  3814.     {
  3815.       /* Non-PC relative relocations which are absolute
  3816.          or which have become non-external now have fixed
  3817.          relocations.  Set the ADD_EXTRA of this relocation
  3818.          to be the relocation we have now determined.  */
  3819.       if (! RELOC_PCREL_P (p))
  3820.         {
  3821.           if ((int)p->r_type <= RELOC_32
  3822.           || RELOC_EXTERN_P (p) == 0)
  3823.         RELOC_ADD_EXTRA (p) = relocation;
  3824.         }
  3825.       /* External PC-relative relocations continue to move around;
  3826.          update their relocations by the amount they have moved
  3827.          so far.  */
  3828.       else if (RELOC_EXTERN_P (p))
  3829.         RELOC_ADD_EXTRA (p) -= pc_relocation;
  3830.       continue;
  3831.     }
  3832. #endif
  3833.  
  3834.       if (RELOC_PCREL_P(p))
  3835.     relocation -= pc_relocation;
  3836.  
  3837.       relocation >>= RELOC_VALUE_RIGHTSHIFT(p);
  3838.  
  3839.       /* Unshifted mask for relocation */
  3840.       mask = 1 << RELOC_TARGET_BITSIZE(p) - 1;
  3841.       mask |= mask - 1;
  3842.       relocation &= mask;
  3843.  
  3844.       /* Shift everything up to where it's going to be used */
  3845.       relocation <<= RELOC_TARGET_BITPOS(p);
  3846.       mask <<= RELOC_TARGET_BITPOS(p);
  3847.  
  3848.       switch (RELOC_TARGET_SIZE(p))
  3849.     {
  3850.     case 0:
  3851.       if (RELOC_MEMORY_SUB_P(p))
  3852.         relocation -= mask & *(char *) (data + addr);
  3853.       else if (RELOC_MEMORY_ADD_P(p))
  3854.         relocation += mask & *(char *) (data + addr);
  3855.       *(char *) (data + addr) &= ~mask;
  3856.       *(char *) (data + addr) |= relocation;
  3857.       break;
  3858.  
  3859.     case 1:
  3860.       if (RELOC_MEMORY_SUB_P(p))
  3861.         relocation -= mask & *(short *) (data + addr);
  3862.       else if (RELOC_MEMORY_ADD_P(p))
  3863.         relocation += mask & *(short *) (data + addr);
  3864.       *(short *) (data + addr) &= ~mask;
  3865.       *(short *) (data + addr) |= relocation;
  3866.       break;
  3867.  
  3868.     case 2:
  3869. #ifndef _CROSS_TARGET_ARCH
  3870.       if (RELOC_MEMORY_SUB_P(p))
  3871.         relocation -= mask & *(long *) (data + addr);
  3872.       else if (RELOC_MEMORY_ADD_P(p))
  3873.         relocation += mask & *(long *) (data + addr);
  3874.       *(long *) (data + addr) &= ~mask;
  3875.       *(long *) (data + addr) |= relocation;
  3876. #else
  3877.     /* Handle long word alignment requirements of SPARC architecture */
  3878.     /* WARNING:  This fix makes an assumption on byte ordering */
  3879.     /* Marc Ullman, Stanford University    Nov. 1 1989  */
  3880.       if (RELOC_MEMORY_SUB_P(p)) {
  3881.         relocation -= mask & 
  3882.           ((*(unsigned short *) (data + addr) << 16) |
  3883.         *(unsigned short *) (data + addr + 2));
  3884.       } else if (RELOC_MEMORY_ADD_P(p)) {
  3885.         relocation += mask &
  3886.           ((*(unsigned short *) (data + addr) << 16) |
  3887.         *(unsigned short *) (data + addr + 2));
  3888.       }
  3889.       *(unsigned short *) (data + addr)     &= (~mask >> 16);
  3890.       *(unsigned short *) (data + addr + 2) &= (~mask & 0xffff);
  3891.       *(unsigned short *) (data + addr)     |= (relocation >> 16);
  3892.       *(unsigned short *) (data + addr + 2) |= (relocation & 0xffff);
  3893. #endif
  3894.       break;
  3895.  
  3896.     default:
  3897.       fatal_with_file ("Unimplemented relocation field length in ", entry);
  3898.     }
  3899.     }
  3900. }
  3901.  
  3902. /* For relocatable_output only: write out the relocation,
  3903.    relocating the addresses-to-be-relocated.  */
  3904.  
  3905. void coptxtrel (), copdatrel ();
  3906.  
  3907. void
  3908. write_rel ()
  3909. {
  3910.   register int i;
  3911.   register int count = 0;
  3912.  
  3913.   if (trace_files)
  3914.     fprintf (stderr, "Writing text relocation:\n\n");
  3915.  
  3916.   /* Assign each global symbol a sequence number, giving the order
  3917.      in which `write_syms' will write it.
  3918.      This is so we can store the proper symbolnum fields
  3919.      in relocation entries we write.  */
  3920.  
  3921.   for (i = 0; i < TABSIZE; i++)
  3922.     {
  3923.       symbol *sp;
  3924.       for (sp = symtab[i]; sp; sp = sp->link)
  3925.     if (sp->referenced || sp->defined)
  3926.       {
  3927.         sp->def_count = count++;
  3928.         /* Leave room for the reference required by N_INDR, if
  3929.            necessary.  */
  3930.         if ((sp->defined & ~N_EXT) == N_INDR)
  3931.           count++;
  3932.       }
  3933.     }
  3934.   /* Correct, because if (relocatable_output), we will also be writing
  3935.      whatever indirect blocks we have.  */
  3936.   if (count != defined_global_sym_count
  3937.       + undefined_global_sym_count + global_indirect_count)
  3938.     fatal ("internal error");
  3939.  
  3940.   /* Write out the relocations of all files, remembered from copy_text.  */
  3941.  
  3942.   each_full_file (coptxtrel, 0);
  3943.  
  3944.   if (trace_files)
  3945.     fprintf (stderr, "\nWriting data relocation:\n\n");
  3946.  
  3947.   each_full_file (copdatrel, 0);
  3948.  
  3949.   if (trace_files)
  3950.     fprintf (stderr, "\n");
  3951. }
  3952.  
  3953. void
  3954. coptxtrel (entry)
  3955.      struct file_entry *entry;
  3956. {
  3957.   register struct relocation_info *p, *end;
  3958.   register int reloc = entry->text_start_address;
  3959.  
  3960.   p = entry->textrel;
  3961.   end = (struct relocation_info *) (entry->header.a_trsize + (char *) p);
  3962.   while (p < end)
  3963.     {
  3964.       RELOC_ADDRESS(p) += reloc;
  3965.       if (RELOC_EXTERN_P(p))
  3966.     {
  3967.       register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist);
  3968.       symbol *symptr = ((symbol *)
  3969.                 (((struct nlist *)
  3970.                   (((char *)entry->symbols) + symindex))
  3971.                  ->n_un.n_name));
  3972.  
  3973.       if (symindex >= entry->header.a_syms)
  3974.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  3975.  
  3976. #ifdef N_INDR
  3977.       /* Resolve indirection.  */
  3978.       if ((symptr->defined & ~N_EXT) == N_INDR)
  3979.         symptr = (symbol *) symptr->value;
  3980. #endif
  3981.  
  3982.       /* If the symbol is now defined, change the external relocation
  3983.          to an internal one.  */
  3984.  
  3985.       if (symptr->defined)
  3986.         {
  3987.           RELOC_EXTERN_P(p) = 0;
  3988.           RELOC_SYMBOL(p) = (symptr->defined & N_TYPE);
  3989. #ifdef RELOC_ADD_EXTRA
  3990.           /* If we aren't going to be adding in the value in
  3991.              memory on the next pass of the loader, then we need
  3992.          to add it in from the relocation entry.  Otherwise
  3993.              the work we did in this pass is lost.  */
  3994.           if (!RELOC_MEMORY_ADD_P(p))
  3995.         RELOC_ADD_EXTRA (p) += symptr->value;
  3996. #endif
  3997.         }
  3998.       else
  3999.         /* Debugger symbols come first, so have to start this
  4000.            after them.  */
  4001.           RELOC_SYMBOL(p) = (symptr->def_count + nsyms
  4002.                  - defined_global_sym_count
  4003.                  - undefined_global_sym_count
  4004.                  - global_indirect_count);
  4005.     }
  4006.       p++;
  4007.     }
  4008.   mywrite (entry->textrel, 1, entry->header.a_trsize, outdesc);
  4009. }
  4010.  
  4011. void
  4012. copdatrel (entry)
  4013.      struct file_entry *entry;
  4014. {
  4015.   register struct relocation_info *p, *end;
  4016.   /* Relocate the address of the relocation.
  4017.      Old address is relative to start of the input file's data section.
  4018.      New address is relative to start of the output file's data section.  */
  4019.   register int reloc = entry->data_start_address - text_size;
  4020.  
  4021.   p = entry->datarel;
  4022.   end = (struct relocation_info *) (entry->header.a_drsize + (char *) p);
  4023.   while (p < end)
  4024.     {
  4025.       RELOC_ADDRESS(p) += reloc;
  4026.       if (RELOC_EXTERN_P(p))
  4027.     {
  4028.       register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist);
  4029.       symbol *symptr = ((symbol *)
  4030.                 (((struct nlist *)
  4031.                   (((char *)entry->symbols) + symindex))
  4032.                  ->n_un.n_name));
  4033.       int symtype;
  4034.  
  4035.       if (symindex >= entry->header.a_syms)
  4036.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  4037.  
  4038. #ifdef N_INDR
  4039.       /* Resolve indirection.  */
  4040.       if ((symptr->defined & ~N_EXT) == N_INDR)
  4041.         symptr = (symbol *) symptr->value;
  4042. #endif
  4043.  
  4044.        symtype = symptr->defined & N_TYPE;
  4045.  
  4046.       if (force_common_definition
  4047.           || symtype == N_DATA || symtype == N_TEXT || symtype == N_ABS)
  4048.         {
  4049.           RELOC_EXTERN_P(p) = 0;
  4050.           RELOC_SYMBOL(p) = symtype;
  4051.         }
  4052.       else
  4053.         /* Debugger symbols come first, so have to start this
  4054.            after them.  */
  4055.         RELOC_SYMBOL(p)
  4056.           = (((symbol *)
  4057.           (((struct nlist *)
  4058.             (((char *)entry->symbols) + symindex))
  4059.            ->n_un.n_name))
  4060.          ->def_count
  4061.          + nsyms - defined_global_sym_count
  4062.          - undefined_global_sym_count
  4063.          - global_indirect_count);
  4064.     }
  4065.       p++;
  4066.     }
  4067.   mywrite (entry->datarel, 1, entry->header.a_drsize, outdesc);
  4068. }
  4069.  
  4070. void write_file_syms ();
  4071. void write_string_table ();
  4072.  
  4073. /* Offsets and current lengths of symbol and string tables in output file. */
  4074.  
  4075. int symbol_table_offset;
  4076. int symbol_table_len;
  4077.  
  4078. /* Address in output file where string table starts.  */
  4079. int string_table_offset;
  4080.  
  4081. /* Offset within string table
  4082.    where the strings in `strtab_vector' should be written.  */
  4083. int string_table_len;
  4084.  
  4085. /* Total size of string table strings allocated so far,
  4086.    including strings in `strtab_vector'.  */
  4087. int strtab_size;
  4088.  
  4089. /* Vector whose elements are strings to be added to the string table.  */
  4090. char **strtab_vector;
  4091.  
  4092. /* Vector whose elements are the lengths of those strings.  */
  4093. int *strtab_lens;
  4094.  
  4095. /* Index in `strtab_vector' at which the next string will be stored.  */
  4096. int strtab_index;
  4097.  
  4098. /* Add the string NAME to the output file string table.
  4099.    Record it in `strtab_vector' to be output later.
  4100.    Return the index within the string table that this string will have.  */
  4101.  
  4102. int
  4103. assign_string_table_index (name)
  4104.      char *name;
  4105. {
  4106.   register int index = strtab_size;
  4107.   register int len = strlen (name) + 1;
  4108.  
  4109.   strtab_size += len;
  4110.   strtab_vector[strtab_index] = name;
  4111.   strtab_lens[strtab_index++] = len;
  4112.  
  4113.   return index;
  4114. }
  4115.  
  4116. FILE *outstream = (FILE *) 0;
  4117.  
  4118. /* Write the contents of `strtab_vector' into the string table.
  4119.    This is done once for each file's local&debugger symbols
  4120.    and once for the global symbols.  */
  4121.  
  4122. void
  4123. write_string_table ()
  4124. {
  4125.   register int i;
  4126.  
  4127.   lseek (outdesc, string_table_offset + string_table_len, 0);
  4128.  
  4129.   if (!outstream)
  4130.     outstream = fdopen (outdesc, "w");
  4131.  
  4132.   for (i = 0; i < strtab_index; i++)
  4133.     {
  4134.       fwrite (strtab_vector[i], 1, strtab_lens[i], outstream);
  4135.       string_table_len += strtab_lens[i];
  4136.     }
  4137.  
  4138.   fflush (outstream);
  4139.  
  4140.   /* Report I/O error such as disk full.  */
  4141.   if (ferror (outstream))
  4142.     perror_name (output_filename);
  4143. }
  4144.  
  4145. /* Write the symbol table and string table of the output file.  */
  4146.  
  4147. void
  4148. write_syms ()
  4149. {
  4150.   /* Number of symbols written so far.  */
  4151.   int syms_written = 0;
  4152.   register int i;
  4153.   register symbol *sp;
  4154.  
  4155.   /* Buffer big enough for all the global symbols.  One
  4156.      extra struct for each indirect symbol to hold the extra reference
  4157.      following. */
  4158.   struct nlist *buf
  4159.     = (struct nlist *) alloca ((defined_global_sym_count
  4160.                 + undefined_global_sym_count
  4161.                 + global_indirect_count)
  4162.                    * sizeof (struct nlist));
  4163.   /* Pointer for storing into BUF.  */
  4164.   register struct nlist *bufp = buf;
  4165.  
  4166.   /* Size of string table includes the bytes that store the size.  */
  4167.   strtab_size = sizeof strtab_size;
  4168.  
  4169.   symbol_table_offset = N_SYMOFF (outheader);
  4170.   symbol_table_len = 0;
  4171.   string_table_offset = N_STROFF (outheader);
  4172.   string_table_len = strtab_size;
  4173.  
  4174.   if (strip_symbols == STRIP_ALL)
  4175.     return;
  4176.  
  4177.   /* Write the local symbols defined by the various files.  */
  4178.  
  4179.   each_file (write_file_syms, &syms_written);
  4180.   file_close ();
  4181.  
  4182.   /* Now write out the global symbols.  */
  4183.  
  4184.   /* Allocate two vectors that record the data to generate the string
  4185.      table from the global symbols written so far.  This must include
  4186.      extra space for the references following indirect outputs. */
  4187.  
  4188.   strtab_vector = (char **) alloca ((num_hash_tab_syms
  4189.                      + global_indirect_count) * sizeof (char *));
  4190.   strtab_lens = (int *) alloca ((num_hash_tab_syms
  4191.                  + global_indirect_count) * sizeof (int));
  4192.   strtab_index = 0;
  4193.  
  4194.   /* Scan the symbol hash table, bucket by bucket.  */
  4195.  
  4196.   for (i = 0; i < TABSIZE; i++)
  4197.     for (sp = symtab[i]; sp; sp = sp->link)
  4198.       {
  4199.     struct nlist nl;
  4200.  
  4201.     nl.n_other = 0;
  4202.     nl.n_desc = 0;
  4203.  
  4204.     /* Compute a `struct nlist' for the symbol.  */
  4205.  
  4206.     if (sp->defined || sp->referenced)
  4207.       {
  4208.         /* common condition needs to be before undefined condition */
  4209.         /* because unallocated commons are set undefined in */
  4210.         /* digest_symbols */
  4211.         if (sp->defined > 1) /* defined with known type */
  4212.           {
  4213.         /* If the target of an indirect symbol has been
  4214.            defined and we are outputting an executable,
  4215.            resolve the indirection; it's no longer needed */
  4216.         if (!relocatable_output
  4217.             && ((sp->defined & N_TYPE) == N_INDR)
  4218.             && (((symbol *) sp->value)->defined > 1))
  4219.           {
  4220.             symbol *newsp = (symbol *) sp->value;
  4221.             nl.n_type = newsp->defined;
  4222.             nl.n_value = newsp->value;
  4223.           }
  4224.         else
  4225.           {
  4226.             nl.n_type = sp->defined;
  4227.             if (sp->defined != (N_INDR | N_EXT))
  4228.               nl.n_value = sp->value;
  4229.             else
  4230.               nl.n_value = 0;
  4231.           }
  4232.           }
  4233.         else if (sp->max_common_size) /* defined as common but not allocated. */
  4234.           {
  4235.         /* happens only with -r and not -d */
  4236.         /* write out a common definition */
  4237.         nl.n_type = N_UNDF | N_EXT;
  4238.         nl.n_value = sp->max_common_size;
  4239.           }
  4240.         else if (!sp->defined)          /* undefined -- legit only if -r */
  4241.           {
  4242.         nl.n_type = N_UNDF | N_EXT;
  4243.         nl.n_value = 0;
  4244.           }
  4245.         else
  4246.           fatal ("internal error: %s defined in mysterious way", sp->name);
  4247.  
  4248.         /* Allocate string table space for the symbol name.  */
  4249.  
  4250.         nl.n_un.n_strx = assign_string_table_index (sp->name);
  4251.  
  4252.         /* Output to the buffer and count it.  */
  4253.  
  4254.         *bufp++ = nl;
  4255.         syms_written++;
  4256.         if (nl.n_type == (N_INDR | N_EXT))
  4257.           {
  4258.         struct nlist xtra_ref;
  4259.         xtra_ref.n_type == N_EXT | N_UNDF;
  4260.         xtra_ref.n_un.n_strx
  4261.           = assign_string_table_index (((symbol *) sp->value)->name);
  4262.         xtra_ref.n_other = 0;
  4263.         xtra_ref.n_desc = 0;
  4264.         xtra_ref.n_value = 0;
  4265.         *bufp++ = xtra_ref;
  4266.         syms_written++;
  4267.           }
  4268.       }
  4269.       }
  4270.  
  4271.   /* Output the buffer full of `struct nlist's.  */
  4272.  
  4273.   lseek (outdesc, symbol_table_offset + symbol_table_len, 0);
  4274.   mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  4275.   symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  4276.  
  4277.   if (syms_written != nsyms)
  4278.     fatal ("internal error: wrong number of symbols written into output file", 0);
  4279.  
  4280.   if (symbol_table_offset + symbol_table_len != string_table_offset)
  4281.     fatal ("internal error: inconsistent symbol table length", 0);
  4282.  
  4283.   /* Now the total string table size is known, so write it.
  4284.      We are already positioned at the right place in the file.  */
  4285.  
  4286.   mywrite (&strtab_size, sizeof (int), 1, outdesc);  /* we're at right place */
  4287.  
  4288.   /* Write the strings for the global symbols.  */
  4289.  
  4290.   write_string_table ();
  4291. }
  4292.  
  4293. /* Write the local and debugger symbols of file ENTRY.
  4294.    Increment *SYMS_WRITTEN_ADDR for each symbol that is written.  */
  4295.  
  4296. /* Note that we do not combine identical names of local symbols.
  4297.    dbx or gdb would be confused if we did that.  */
  4298.  
  4299. void
  4300. write_file_syms (entry, syms_written_addr)
  4301.      struct file_entry *entry;
  4302.      int *syms_written_addr;
  4303. {
  4304.   register struct nlist *p = entry->symbols;
  4305.   register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
  4306.  
  4307.   /* Buffer to accumulate all the syms before writing them.
  4308.      It has one extra slot for the local symbol we generate here.  */
  4309.   struct nlist *buf
  4310.     = (struct nlist *) alloca (entry->header.a_syms + sizeof (struct nlist));
  4311.   register struct nlist *bufp = buf;
  4312.  
  4313.   /* Upper bound on number of syms to be written here.  */
  4314.   int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1;
  4315.  
  4316.   /* Make tables that record, for each symbol, its name and its name's length.
  4317.      The elements are filled in by `assign_string_table_index'.  */
  4318.  
  4319.   strtab_vector = (char **) alloca (max_syms * sizeof (char *));
  4320.   strtab_lens = (int *) alloca (max_syms * sizeof (int));
  4321.   strtab_index = 0;
  4322.  
  4323.   /* Generate a local symbol for the start of this file's text.  */
  4324.  
  4325.   if (discard_locals != DISCARD_ALL)
  4326.     {
  4327.       struct nlist nl;
  4328.  
  4329.       nl.n_type = N_TEXT;
  4330.       nl.n_un.n_strx = assign_string_table_index (entry->local_sym_name);
  4331.       nl.n_value = entry->text_start_address;
  4332.       nl.n_desc = 0;
  4333.       nl.n_other = 0;
  4334.       *bufp++ = nl;
  4335.       (*syms_written_addr)++;
  4336.       entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist);
  4337.     }
  4338.  
  4339.   /* Read the file's string table.  */
  4340.  
  4341.   entry->strings = (char *) alloca (entry->string_size);
  4342.   read_entry_strings (file_open (entry), entry);
  4343.  
  4344.   for (; p < end; p++)
  4345.     {
  4346.       register int type = p->n_type;
  4347.       register int write = 0;
  4348.  
  4349.       /* WRITE gets 1 for a non-global symbol that should be written.  */
  4350.  
  4351.  
  4352.       if (SET_ELEMENT_P (type))    /* This occurs even if global.  These */
  4353.                 /* types of symbols are never written */
  4354.                 /* globally, though they are stored */
  4355.                 /* globally.  */
  4356.         write = relocatable_output;
  4357.       else if (!(type & (N_STAB | N_EXT)))
  4358.         /* ordinary local symbol */
  4359.     write = ((discard_locals != DISCARD_ALL)
  4360.          && !(discard_locals == DISCARD_L &&
  4361.               (p->n_un.n_strx + entry->strings)[0] == LPREFIX)
  4362.          && type != N_WARNING);
  4363.       else if (!(type & N_EXT))
  4364.     /* debugger symbol */
  4365.         write = (strip_symbols == STRIP_NONE);
  4366.  
  4367.       if (write)
  4368.     {
  4369.       /* If this symbol has a name,
  4370.          allocate space for it in the output string table.  */
  4371.  
  4372.       if (p->n_un.n_strx)
  4373.         p->n_un.n_strx = assign_string_table_index (p->n_un.n_strx
  4374.                             + entry->strings);
  4375.  
  4376.       /* Output this symbol to the buffer and count it.  */
  4377.  
  4378.       *bufp++ = *p;
  4379.       (*syms_written_addr)++;
  4380.     }
  4381.     }
  4382.  
  4383.   /* All the symbols are now in BUF; write them.  */
  4384.  
  4385.   lseek (outdesc, symbol_table_offset + symbol_table_len, 0);
  4386.   mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  4387.   symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  4388.  
  4389.   /* Write the string-table data for the symbols just written,
  4390.      using the data in vectors `strtab_vector' and `strtab_lens'.  */
  4391.  
  4392.   write_string_table ();
  4393.   entry->strings = 0;        /* Since it will dissapear anyway.  */
  4394. }
  4395.  
  4396. /* Copy any GDB symbol segments from the input files to the output file.
  4397.    The contents of the symbol segment is copied without change
  4398.    except that we store some information into the beginning of it.  */
  4399.  
  4400. void write_file_symseg ();
  4401.  
  4402. void
  4403. write_symsegs ()
  4404. {
  4405.   each_file (write_file_symseg, 0);
  4406. }
  4407.  
  4408. void
  4409. write_file_symseg (entry)
  4410.      struct file_entry *entry;
  4411. {
  4412.   char buffer[4096];
  4413.   struct symbol_root root;
  4414.   int indesc;
  4415.   int len;
  4416.  
  4417.   if (entry->symseg_offset == 0)
  4418.     return;
  4419.  
  4420.   /* This entry has a symbol segment.  Read the root of the segment.  */
  4421.  
  4422.   indesc = file_open (entry);
  4423.   lseek (indesc, entry->symseg_offset + entry->starting_offset, 0);
  4424.   if (sizeof root != read (indesc, &root, sizeof root))
  4425.     fatal_with_file ("premature end of file in symbol segment of ", entry);
  4426.  
  4427.   /* Store some relocation info into the root.  */
  4428.  
  4429.   root.ldsymoff = entry->local_syms_offset;
  4430.   root.textrel = entry->text_start_address;
  4431.   root.datarel = entry->data_start_address - entry->header.a_text;
  4432.   root.bssrel = entry->bss_start_address
  4433.     - entry->header.a_text - entry->header.a_data;
  4434.   root.databeg = entry->data_start_address - root.datarel;
  4435.   root.bssbeg = entry->bss_start_address - root.bssrel;
  4436.  
  4437.   /* Write the modified root into the output file.  */
  4438.  
  4439.   mywrite (&root, sizeof root, 1, outdesc);
  4440.  
  4441.   /* Copy the rest of the symbol segment unchanged.  */
  4442.  
  4443.   if (entry->superfile)
  4444.     {
  4445.       /* Library member: number of bytes to copy is determined
  4446.      from the member's total size.  */
  4447.  
  4448.       int total = entry->total_size - entry->symseg_offset - sizeof root;
  4449.  
  4450.       while (total > 0)
  4451.     {
  4452.       len = read (indesc, buffer, min (sizeof buffer, total));
  4453.  
  4454.       if (len != min (sizeof buffer, total))
  4455.         fatal_with_file ("premature end of file in symbol segment of ", entry);
  4456.       total -= len;
  4457.       mywrite (buffer, len, 1, outdesc);
  4458.     }
  4459.     }
  4460.   else
  4461.     {
  4462.       /* A separate file: copy until end of file.  */
  4463.  
  4464.       while (len = read (indesc, buffer, sizeof buffer))
  4465.     {
  4466.       mywrite (buffer, len, 1, outdesc);
  4467.       if (len < sizeof buffer)
  4468.         break;
  4469.     }
  4470.     }
  4471.  
  4472.   file_close ();
  4473. }
  4474.  
  4475. /* Create the symbol table entries for `etext', `edata' and `end'.  */
  4476.  
  4477. void
  4478. symtab_init ()
  4479. {
  4480. #ifndef nounderscore
  4481.   edata_symbol = getsym ("_edata");
  4482.   etext_symbol = getsym ("_etext");
  4483.   end_symbol = getsym ("_end");
  4484. #else
  4485.   edata_symbol = getsym ("edata");
  4486.   etext_symbol = getsym ("etext");
  4487.   end_symbol = getsym ("end");
  4488. #endif
  4489.  
  4490. #ifdef sun
  4491.   {
  4492.     symbol *dynamic_symbol = getsym ("__DYNAMIC");
  4493.     dynamic_symbol->defined = N_ABS | N_EXT;
  4494.     dynamic_symbol->referenced = 1;
  4495.     dynamic_symbol->value = 0;
  4496.   }
  4497. #endif
  4498.  
  4499. #ifdef sequent
  4500.   {
  4501.     symbol *_387_flt_symbol = getsym ("_387_flt");
  4502.     _387_flt_symbol->defined = N_ABS | N_EXT;
  4503.     _387_flt_symbol->referenced = 1;
  4504.     _387_flt_symbol->value = 0;
  4505.   }
  4506. #endif
  4507.  
  4508.   edata_symbol->defined = N_DATA | N_EXT;
  4509.   etext_symbol->defined = N_TEXT | N_EXT;
  4510.   end_symbol->defined = N_BSS | N_EXT;
  4511.  
  4512.   edata_symbol->referenced = 1;
  4513.   etext_symbol->referenced = 1;
  4514.   end_symbol->referenced = 1;
  4515. }
  4516.  
  4517. /* Compute the hash code for symbol name KEY.  */
  4518.  
  4519. int
  4520. hash_string (key)
  4521.      char *key;
  4522. {
  4523.   register char *cp;
  4524.   register int k;
  4525.  
  4526.   cp = key;
  4527.   k = 0;
  4528.   while (*cp)
  4529.     k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
  4530.  
  4531.   return k;
  4532. }
  4533.  
  4534. /* Get the symbol table entry for the global symbol named KEY.
  4535.    Create one if there is none.  */
  4536.  
  4537. symbol *
  4538. getsym (key)
  4539.      char *key;
  4540. {
  4541.   register int hashval;
  4542.   register symbol *bp;
  4543.  
  4544.   /* Determine the proper bucket.  */
  4545.  
  4546.   hashval = hash_string (key) % TABSIZE;
  4547.  
  4548.   /* Search the bucket.  */
  4549.  
  4550.   for (bp = symtab[hashval]; bp; bp = bp->link)
  4551.     if (! strcmp (key, bp->name))
  4552.       return bp;
  4553.  
  4554.   /* Nothing was found; create a new symbol table entry.  */
  4555.  
  4556.   bp = (symbol *) xmalloc (sizeof (symbol));
  4557.   bp->refs = 0;
  4558.   bp->name = (char *) xmalloc (strlen (key) + 1);
  4559.   strcpy (bp->name, key);
  4560.   bp->defined = 0;
  4561.   bp->referenced = 0;
  4562.   bp->trace = 0;
  4563.   bp->value = 0;
  4564.   bp->max_common_size = 0;
  4565.   bp->warning = 0;
  4566.   bp->undef_refs = 0;
  4567.   bp->multiply_defined = 0;
  4568.  
  4569.   /* Add the entry to the bucket.  */
  4570.  
  4571.   bp->link = symtab[hashval];
  4572.   symtab[hashval] = bp;
  4573.  
  4574.   ++num_hash_tab_syms;
  4575.  
  4576.   return bp;
  4577. }
  4578.  
  4579. /* Like `getsym' but return 0 if the symbol is not already known.  */
  4580.  
  4581. symbol *
  4582. getsym_soft (key)
  4583.      char *key;
  4584. {
  4585.   register int hashval;
  4586.   register symbol *bp;
  4587.  
  4588.   /* Determine which bucket.  */
  4589.  
  4590.   hashval = hash_string (key) % TABSIZE;
  4591.  
  4592.   /* Search the bucket.  */
  4593.  
  4594.   for (bp = symtab[hashval]; bp; bp = bp->link)
  4595.     if (! strcmp (key, bp->name))
  4596.       return bp;
  4597.  
  4598.   return 0;
  4599. }
  4600.  
  4601. /* Report a fatal error.
  4602.    STRING is a printf format string and ARG is one arg for it.  */
  4603.  
  4604. void
  4605. fatal (string, arg)
  4606.      char *string, *arg;
  4607. {
  4608.   fprintf (stderr, "ld: ");
  4609.   fprintf (stderr, string, arg);
  4610.   fprintf (stderr, "\n");
  4611.   exit (1);
  4612. }
  4613.  
  4614. /* Report a fatal error.  The error message is STRING
  4615.    followed by the filename of ENTRY.  */
  4616.  
  4617. void
  4618. fatal_with_file (string, entry)
  4619.      char *string;
  4620.      struct file_entry *entry;
  4621. {
  4622.   fprintf (stderr, "ld: ");
  4623.   fprintf (stderr, string);
  4624.   print_file_name (entry, stderr);
  4625.   fprintf (stderr, "\n");
  4626.   exit (1);
  4627. }
  4628.  
  4629. /* Report a fatal error using the message for the last failed system call,
  4630.    followed by the string NAME.  */
  4631.  
  4632. void
  4633. perror_name (name)
  4634.      char *name;
  4635. {
  4636.   extern int errno, sys_nerr;
  4637.   extern char *sys_errlist[];
  4638.   char *s;
  4639.  
  4640.   if (errno < sys_nerr)
  4641.     s = concat ("", sys_errlist[errno], " for %s");
  4642.   else
  4643.     s = "cannot open %s";
  4644.   fatal (s, name);
  4645. }
  4646.  
  4647. /* Report a fatal error using the message for the last failed system call,
  4648.    followed by the name of file ENTRY.  */
  4649.  
  4650. void
  4651. perror_file (entry)
  4652.      struct file_entry *entry;
  4653. {
  4654.   extern int errno, sys_nerr;
  4655.   extern char *sys_errlist[];
  4656.   char *s;
  4657.  
  4658.   if (errno < sys_nerr)
  4659.     s = concat ("", sys_errlist[errno], " for ");
  4660.   else
  4661.     s = "cannot open ";
  4662.   fatal_with_file (s, entry);
  4663. }
  4664.  
  4665. /* Report a nonfatal error.
  4666.    STRING is a format for printf, and ARG1 ... ARG3 are args for it.  */
  4667.  
  4668. void
  4669. error (string, arg1, arg2, arg3)
  4670.      char *string, *arg1, *arg2, *arg3;
  4671. {
  4672.   fprintf (stderr, "%s: ", progname);
  4673.   fprintf (stderr, string, arg1, arg2, arg3);
  4674.   fprintf (stderr, "\n");
  4675. }
  4676.  
  4677.  
  4678. /* Output COUNT*ELTSIZE bytes of data at BUF
  4679.    to the descriptor DESC.  */
  4680.  
  4681. void
  4682. mywrite (buf, count, eltsize, desc)
  4683.      char *buf;
  4684.      int count;
  4685.      int eltsize;
  4686.      int desc;
  4687. {
  4688.   register int val;
  4689.   register int bytes = count * eltsize;
  4690.  
  4691.   while (bytes > 0)
  4692.     {
  4693.       val = write (desc, buf, bytes);
  4694.       if (val <= 0)
  4695.     perror_name (output_filename);
  4696.       buf += val;
  4697.       bytes -= val;
  4698.     }
  4699. }
  4700.  
  4701. /* Output PADDING zero-bytes to descriptor OUTDESC.
  4702.    PADDING may be negative; in that case, do nothing.  */
  4703.  
  4704. void
  4705. padfile (padding, outdesc)
  4706.      int padding;
  4707.      int outdesc;
  4708. {
  4709.   register char *buf;
  4710.   if (padding <= 0)
  4711.     return;
  4712.  
  4713.   buf = (char *) alloca (padding);
  4714.   bzero (buf, padding);
  4715.   mywrite (buf, padding, 1, outdesc);
  4716. }
  4717.  
  4718. /* Return a newly-allocated string
  4719.    whose contents concatenate the strings S1, S2, S3.  */
  4720.  
  4721. char *
  4722. concat (s1, s2, s3)
  4723.      char *s1, *s2, *s3;
  4724. {
  4725.   register int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  4726.   register char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  4727.  
  4728.   strcpy (result, s1);
  4729.   strcpy (result + len1, s2);
  4730.   strcpy (result + len1 + len2, s3);
  4731.   result[len1 + len2 + len3] = 0;
  4732.  
  4733.   return result;
  4734. }
  4735.  
  4736. /* Parse the string ARG using scanf format FORMAT, and return the result.
  4737.    If it does not parse, report fatal error
  4738.    generating the error message using format string ERROR and ARG as arg.  */
  4739.  
  4740. int
  4741. parse (arg, format, error)
  4742.      char *arg, *format;
  4743. {
  4744.   int x;
  4745.   if (1 != sscanf (arg, format, &x))
  4746.     fatal (error, arg);
  4747.   return x;
  4748. }
  4749.  
  4750. /* Like malloc but get fatal error if memory is exhausted.  */
  4751.  
  4752. int
  4753. xmalloc (size)
  4754.      int size;
  4755. {
  4756.   register int result = malloc (size);
  4757.   if (!result)
  4758.     fatal ("virtual memory exhausted", 0);
  4759.   return result;
  4760. }
  4761.  
  4762. /* Like realloc but get fatal error if memory is exhausted.  */
  4763.  
  4764. int
  4765. xrealloc (ptr, size)
  4766.      char *ptr;
  4767.      int size;
  4768. {
  4769.   register int result = realloc (ptr, size);
  4770.   if (!result)
  4771.     fatal ("virtual memory exhausted", 0);
  4772.   return result;
  4773. }
  4774.  
  4775. #ifdef USG
  4776. #ifndef GNUDOS
  4777.  
  4778. void
  4779. bzero (p, n)
  4780.      char *p;
  4781. {
  4782.   memset (p, 0, n);
  4783. }
  4784.  
  4785. void
  4786. bcopy (from, to, n)
  4787.      char *from, *to;
  4788. {
  4789.   memcpy (to, from, n);
  4790. }
  4791.  
  4792. getpagesize ()
  4793. {
  4794.   return (4096);
  4795. }
  4796.  
  4797. #endif
  4798. #endif
  4799.  
  4800. #if TARGET == SUN4
  4801.  
  4802. /* Don't use local pagesize to build for Sparc.  */
  4803.  
  4804. getpagesize ()
  4805. {
  4806.   return (8192);
  4807. }
  4808. #endif
  4809.